Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.history.back() not working.

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> 
like image 392
Darran Smith Avatar asked Sep 04 '14 19:09

Darran Smith


People also ask

Why does Windows history back not work?

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.

Can I use history back ()?

back() is the same as history.go(-1) . history. back() is the same as clicking "Back" your browser.

What is history Go (- 1?

The History. back() method causes the browser to move back one page in the session history. It has the same effect as calling history.go(-1) . If there is no previous page, this method call does nothing. This method is asynchronous.

How do I go back to an earlier page in HTML?

You can use the history. back() method to tell the browser to go back to the user's previous page.


3 Answers

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.

like image 128
giulp Avatar answered Oct 20 '22 05:10

giulp


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.

like image 4
l0gicgate Avatar answered Oct 20 '22 05:10

l0gicgate


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

like image 4
tony Avatar answered Oct 20 '22 07:10

tony