Imran Akram's blog

Let's talk about life, technicalities, career opportunities, current affairs and a lot more!

How to go back to the previous page using Javascript

Posted on | June 26, 2008 | 4 Comments

well, this is how you can go back to the previous page through Javascript.

javascript:history.go(-1);

  • Share/Bookmark

Comments

4 Responses to “How to go back to the previous page using Javascript”

  1. James C.
    July 5th, 2008 @ 9:29 pm

    Going back is easy enough, but what if there is nothing to go back to? I would like to be able to detect if history.go(-1) will actually do anything. If not, have a disable back button displayed.

  2. imak47
    July 6th, 2008 @ 11:53 am

    if there’s nothin to go back to it doesnt do anything… as far as disabling the back button displayed it concerned. Try checking the length of the history object.

    if (history.length) …
    check *may* tell you if browser has history.length property and if
    there was *any* navigation during the current session. The latter check
    may also fail easily if the previous page was the first in the session
    and if you came on the current page via replace() method supported by
    some browsers.

    So history.length doesn’t give you any idea where (by the index
    position) are you now and its counter is not accurate in many
    circumstances.

    if (history.length) {history.go(-1);} is just additional check to avoid
    some (but not all at all) situations.

    I’d suggest u use ur own way of navigation for that. Using Sitemap objects in ASP .NET would be mighty helpful I think. Wish u all the best

  3. ChaotiX66
    January 6th, 2009 @ 11:21 am

    Do you have any suggestions in using the “back javascript” on wordpress.com? Seems like they allow a limited number of javascripts by users on their blogs.

  4. imak47
    January 6th, 2009 @ 11:42 am

    Thanks for dropping by. I really think WordPress wont allow that anyway bcz if they allow users to put JS on freely they can do lots of things WordPress doesnt want them to be doing like adding ads etc.

Leave a Reply