11

I cant get this to work. I have been trying for ages. Please help me.

<script>
function goBack() {
    window.history.back()
}
</script>

<button onclick="goBack()">Go Back</button> 
2
  • 4
    use window.history.go(-1)
    – Sunand
    Sep 4, 2014 at 19:22
  • does console say that goBack() is undefined? because window.history.back() works fine. Sep 4, 2014 at 19:34

8 Answers 8

10

Please have a look at this question: Inconsistency with window.history.back().

this

<button type="button" onclick="goBack()">Go Back</button> 

could be what you're looking for

As Kevin B suggests

The browser could be interpreting the button as a submit button and submitting the form, thus causing a page refresh. Adding type="button" will prevent that.

1
  • thank you so much, i couldnt figure it out for so long time. it works perfectly! Aug 19, 2022 at 19:51
4

First of all, you should make sure that your script tag have the proper type set:

<script type="text/javascript">
...
</script>

Also, I would suggest using the "go" function of the history object instead since the compatibility is higher. To simplify things you can simply do this:

<button onclick="javascript:history.go(-1)">Go Back</button>

Hope this helps.

4

This can stop it appearing to work

<a href="#" onclick=DoSomething()>Stop it working</a>

That's because the href will effectively add a new page in the history, the present page again

Incidently if you call

event.preventDefault()

in DoSomething then # will never get added to the history and it will appear to work again

4

This worked for me

    <button type="reset" onclick="goBack()">Go Back</button> 
2
<a href="#" onclick="history.back();return false;">Go Back</a>

The 'return false;' bit helped fix it for me

1

The window.history.back() method does not work if it does not have any previous URL to go to. Your program needs to have a start URL and you will need to have your program moving forward to the next page to be able to go back and forward.

1

Try preventing the default action of the button. Note that nothing will happen if you have accessed no other pages in the current tab.

Javascript:

<button id="goBack">Go Back</button>
<span id="result"></span>
<script>
document.getElementById("goBack").addEventListener("click", function(event){
event.preventDefault();
document.getElementById("result").innerHTML = "Going back";
window.history.back();
});
</script>

jQuery:

<button id="goBack">Go Back</button>
<span id="result"></span>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$('#goBack').click(function(event){
event.preventDefault();
document.getElementById("result").innerHTML = "Going back";
window.history.back();
});
</script>

0

Actually for working of this you need to redirect to this website from any other website or if you are doing on local html file then you can just open any website and paste the link of your local file in search bar in that tab(don't open a new tab) and then it will work. Basically this fuction just press the back button of your browser so you need to have something so that it can go back.

1
  • 1
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Aug 18, 2022 at 12:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.