Search code examples
javascriptbrowser-history

Moving back webpages with Javascript


Ok this is the issue. I am working in a popup. We go from Page 1 to Page 2. Page 2 has frames set up on it. I know it's bad but it was not my decisions. Now on Page 2 in one frame we continue onto Page 3. If I click the back button on Page 2 with javascript history.go(-1) we can get to Page 1. But if the user goes from Page 2 to Page 3 then back to Page 2 a user can not simple go back to Page 1 unless the javascript is history.go(-3). Is there a way I can tell where I am coming from so I can determine which go to use?

FYI URLRefferrer does not work but it only gives me the page holding all the frames. Any suggestions welcomed.


Solution

  • An easy way is to link to the page you want directly instead of using history.go. Alternatively, you can try to use parent.history.go(-1).

    A third thing you may try, if you have complete control over all the links in the inner frame is to always use location.replace from the inner frame, which will not add an entry into the history, something like the following script

    $(function() {
        $('a').click(function(e) {
            e.preventDefault();
            location.replace(this.href);
        });
    });