Buttons in Javascript

Asked

Viewed 1,418 times

0

I have a page with save button, but every time it is clicked the script appears that the information has been saved and leaves the page. My goal is that he doesn’t leave, but I looked at the code and I didn’t understand why it was coming out. Follow the button:

<div class="row">
        <div class="col-md-6 botao">
            <button type="submit" class="btn btn-primary form-control" name="button" id="button">Salvar</button>
        </div>
        <div class="col-md-6 botao" >
            <button type="button" name="button" id="button" class="btn btn-danger form-control">Cancelar</button>
        </div>

Follows the Script:

<script>
    var retorna = function () {
        window.location = "a_pagina_anterior.php";
    }

    var button = document.querySelector("#button"); 
    button.onclick = retorna;
</script>
  • 1

    Are some chunk of code missing ? As far as I remember history is not a function, history you can call back() and forward()

  • No. I use sublime text and it accuses when something is recognized or not as a function. I was trying to use window.history.back()

  • Open a console in some browser and type window.history()

  • Actually, it’s not a function. But I don’t understand why my window.history.back() doesn’t work.

  • Now yes kkk, your back doesn’t work ?

  • Repeat your question again so I can help you better.

  • Right. When my save button is clicked, the page returns to the previous one. The code in jscript is this one, I looked several times and it seems all right, I even tried to disable the script but it keeps happening the same.

  • Mariana, the code you are using is really what is in the question? review the code

  • Opa. correction, window.history.back() no, window.history.forward()

  • Code you posted from error because it uses history as Function.

  • Ah, you’re right, the code is wrong. That’s the code I’ve already touched until I say enough, I’ll edit to the original.

  • All right, as soon as you change communicate, try to put the whole code ok?

Show 7 more comments

2 answers

1

Answering your question, it leaves the page because you are using the

window.location

Explanation:

The window.Location object can be used to get the address of the current page (URL) and to redirect the browser to a new page.

  • Yes, I tried to put the address of the page itself that I want to keep but keeps coming back on the previous page

  • As I explained window.Location will yes change the independent page if you pass the same page or a different one.

  • You can study a little about ajax and use to make your registration, so you stay on the page.

  • Okay, thanks for your help.

  • You are welcome, anything research crud with ajax and php, I saw that you use php, if my answer answered your question do not forget to evaluate it, until next.

  • I still don’t have 15 reputation points.

  • It’s okay, I just commented because I always do it kkk, thanks and needing we’ll be here.

Show 2 more comments

0

You need to use window.open and configure it according to your need:

<script>
 var windowObjectReference;
 var strWindowFeatures ="menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes";

 var retorna = function () {
    windowObjectReference = window.open("a_pagina_anterior.php", "_blank ", strWindowFeatures);
}

var button = document.querySelector("#button"); 
button.onclick = retorna;

The second window.open parameter is the mode in which it will be that they are:

_blank - O link abrirá em uma nova janela
_self - O link abrirá no mesmo frame onde foi clicado
_parent - O link abrirá no frameset pai
_top - O link abrirá na mesma janela, sem frames

The strWindowFeatures variable refers to the settings of the new window: for more details: Window.open

  • So the problem is that it opens a new page similar to the original one when I use the snippet that you answered. what should happen is to simply save the changes without leaving the page.

  • @Marianaferreira uses _self, and another, his php already works?

  • It works. I’ll try.

  • I used the _self and nothing happened. I’m sorry the question, I’m kind of a layperson in the language, but this is Object Orientation?

  • Tranquila @Marianaferreira, is object oriented yes.

  • @Marianaferreira Edit the question and post your code, maybe it will be easier.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.