History.back() – Problems With Chrome and Safari

Had a bit of an issue today with our old friend history.back(). This works fine in IE + FF:

<a href=”#” onclick=”history.back();”>Back</a>

But doesn’t seem to want to play in Chrome or Safari. So I tried to tidy up the code and be a little more strict:

<a href=”#” onclick=”window.history.back(-1);”>Back</a>

But still noy joy. In the end I went for this:

<script>
function goback() {
    history.go(-1);
}
</script>

<a href=”javascript:goback()”>Back</a>

Which works across every browser, as far as I can tell.

Not sure if this is my error or a Chrome bug, but this is a useful workaround for me anyway. Hope it helps.

Comments

  1. Thanks, dude! You’ve saved at least couple of my head kicking the wall :)

  2. Thank you very much! You solved my huge problem.

  3. Actually, I find this is not really working, and sometime, it broke.

  4. simonthegeek says:

    Hi Taiji,

    What browser are you using? Which version?

    This totally works for me.

  5. this code:
    window.history.back(-1);

    works for:
    chrome 4.1
    safari 4.0.5
    ie 8

    so what version of browser is it not working for you?

  6. simonthegeek says:

    Hi Simon,

    When this article was written, we were on Chrome 2.1 and Firefox 3.0 (I think), and they were having the problems mentioned.

    May well be that this is fixed now, not sure, haven’t come across it for a while.

  7. simon_the_third says:

    window.back(); doesnt’t work in Chrome 5.0 but does in FireFox 3.6.6, IE7&8.

    window.history.back(-1); works in them all.

    Thanks to simon for this code!!

  8. I had to use the original fix (the javascript function) on Chrome 5.0.375.99 on Vista 32.

    None of the other methods would work here.

  9. No funciona en Google Chrome 5

  10. Hi All,

    i am using window .history.back() on my website and deployed it on linux server , the window.history.back() works fine on my local system but its is not working on the server. So do we need to make certain other arrangement for linux server on moving with history.back() oe there is any other workaround.
    Kindly reply asap.

    Regards

    • simonthegeek says:

      What browser are you using Satish?

      This is a browser issue only, should not matter what server you are using.

      Just remember when testing to actually visit some other pages first, before clicking the link to go back, otherwise you won’t have anywhere to go back to!

  11. Hi i am using IE 8 only and i reach to that particular page just after going through two to three pages.
    It works fine on IE 7 but is not working and giving some error on IE 8.

    Kindly suggest.

    • simonthegeek says:

      Hi Satish,

      Have you tried the fix mentioned at the top of this page? Use this code:

      <script>
      function goback() {
          history.go(-1);
      }
      </script>

      <a href=”javascript:goback()”>Back</a>

      Let me know how you get on.

  12. Hi All

    Have you tried this code on google chrome.
    Ohhhh Not Yet It’s not Gonna Work on it!! :(

    Please Check it out

    history.back(); and history.go(-1);

  13. simonthegeek says:

    Hi Vikash,

    You will have problems with history.back(); and history.go(-1); – please use the javascript fix in the article at the top of this page.

    Thanks,

    Simon

  14. simonthegeek says:

    Or…. post your code here and we will try to work out what is going wrong.

  15. Ich habe einen crazy fix gemacht für IE (für seiten mit POST data in filter select ):

    var browserName=navigator.appName;

    if (browserName==”Microsoft Internet Explorer”)
    {
    document.write(‘ back ‘);
    }
    else
    {
    document.write(‘ back‘);
    }

    function goback()
    {
    history.go(0);
    }

  16. This seems to work in Chrome. Be sure to return false to cancel the navigation:

  17. need a fix for safari 5.0

  18. todoselmundo says:

    Hi,
    Has anyone solved the problem with chrome/safari?

    Tnks
    bye

  19. Back
    This worked for me in Safari as well as in Firefox

  20. "Back"
    
    This worked for me in Safari as well as in Firefox.
  21. Thanks Kevin, I was with this problem and add ”return false;” fix the problem!

    I would not like to use Back since I like to separate my html from the javascript.

  22. Just correcting, where you see the link Back, read href=”javascript:goback()”. It was not my intention create a link in my previous comment. :S

  23. Great solution thanks, I had this issue which was doing my head in but this fixed it nicely.

  24. Thanks Kevin. Your solutions was great!! Now it works winth Chrome and Firefox.

    AlexQ.
    —————————————————-
    Kevin Says: On November 15th, 2010 at 8:03 pm.

    This seems to work in Chrome. Be sure to return false to cancel the navigation:Back

  25. You don't need the extra function. Your second option was:

    Back

    but, it should have been:

    Back

  26. "history.back(-1); return false;" should work everywhere.

    "return false;" suppresses default behavior, which seems to otherwise suppress any navigation in Chrome and Safari, probably to allow event bubbling, or to implement the navigation to the '#' bookmark href.

    rel="nofollow" is probably not a bad idea either though.

    Hope this helps,

    Matt.

  27. working fine.Thank you

  28. Thanabalan says:

    Thanks Rodrigo . Working fine

  29. This was helpful!

    Was facing a challenge with using window.history.back() in Safari where it was cancelling the POST request on the previous page.

    Thanks!

  30. Thanks for all the nice hints. I figured out that actually the "return false" mentioned by Matt is the easiest solution and resolving the main "problem" of safari that it will execute the link href… what is right in my opinion…

    anyway , this works for me:
    <a href="#" onclick="window.history.back();return false;">back</a>

  31. Arsh Bajpai says:

    Use this code hope it will make happy
    var referrer = document.referrer;
    window.location.replace(referrer);

  32. Hello! Thank you for your help, but could anybody answer me why $history.back() worked for me on all browsers (chrome, ff, safari) but for some reason $window.history.back() didn't work on safari. I tried googling it with no luck.