Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
13
rated 0 times [  15] [ 2]  / answers: 1 / hits: 18385  / 11 Years ago, fri, december 6, 2013, 12:00:00

I don't understand why in Firefox, window.history.back() does work on a button:



<button onclick=window.history.back()>Go back</button>


But it does not work for a link:



<a onclick=window.history.back()>Go back</a>


What is the difference?



An example illustrating this:



index.html



<!DOCTYPE html>
<html>
<body>
<h1>First page</h1>
<br/>
<a href=second.html>Second</a>
</body>
</html>


second.html



<!DOCTYPE html>
<html>
<body>
<h1>Second page</h1>
<a href=third.html>Third</a>
<br/>
<br/>
<button onclick=window.history.back()>Go Back (button)</button>
<br/>
<a href=javascript: void(0); onclick=window.history.back()>Go Back (a)</a>
</body>
</html>


third.html



<!DOCTYPE html>
<html>
<body>
<h1>Third page</h1>
<br/>
<button onclick=window.history.back()>Go Back (button)</button>
<br/>
<a href=javascript: void(0); onclick=window.history.back()>Go Back (a)</a>
</body>
</html>


Scenario:




  1. Run index.html and click Second.

  2. On second.html click Third.

  3. On third.html click Go back (a).

  4. On second.html click Go back (a). Boom! I'm on the third.html, and not on the first.html!!!



Plunker example (Note! Run it on Firefox)



If you use Go back (button) it works. What is the difference in <a onclick and <button onclick in this case?


More From » html

 Answers
12

You are facing this issue (quoted from vikku.info):




But what had happened when i press the back button after navigating to
various pages was i got locked between the last two navigated pages.




Someone in the comments hit the nail on this issue.




  • When you create the onclick attribute, you are attaching an additional event handler for clicking a link. However, the browser will still handle some native logic, so it will still add the current page to the history;

  • When you pass in specific javascript into the href attribute, you are actually overriding the native logic of the browser, so it won't add the current page to the history;



SIMPLE, DIRTY SOLUTION



HTML:



<a href=javascript:window.history.back();>Back</a>





IMPROVED SOLUTION



I've also created an example (Plunker example) that makes use of the native preventDefault functionality (MDN on preventDefault). In that case, it is not needed to write javascript in the href attribute. Now, you can support users that are not using javascript by linking to, for example, the homepage. You better also avoid using inline event handlers.



HTML:



<a href=index.html id=backButton>Back</a>


Javascript:



var backbutton = document.getElementById(backButton);
backbutton.onclick = function(e){
e = e || window.event; // support for IE8 and lower
e.preventDefault(); // stop browser from doing native logic
window.history.back();
}

[#73877] Thursday, December 5, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tierney

Total Points: 45
Total Questions: 101
Total Answers: 94

Location: Sudan
Member since Thu, May 7, 2020
4 Years ago
;