-2
I’m trying to make a page that, when clicked, creates a button that when pressed it calls the function back in the history:
function goBack(){
window.history.back();
}
function criarBotao(){
var botao = document.createElement("BUTTON");
botao.innerHTML = "Voltar";
document.body.appendChild(botao);
botao.onclick = goBack();
}
window.onload = criarBotao();
The problem is that every time the page opens, the goBack() function happens automatically, without pressing the button. Could anyone help me? Thanks.
Should be
botao.onclick = goBack;
without parentheses. Writing the function name with parentheses will invoke it, you don’t want to invoke it, you just want to pass it to the event. The same goes forcriarBotao
, should bewindow.onload = criarBotao;
– Andre
Thanks friend.
– Rodrigo Müller