Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript / jQuery back button - so long as last page was part of current site?

With the code below I can make a 'back' button, but is there a way of making the link require that the last page was part of the current site?

$(document).ready(function(){
    $('a.back').click(function(){
        parent.history.back();
        return false;
    });
});

If the last page wasn't part of the current site then ideally id like to be able to specify a backup link.

Thanks

like image 695
Evanss Avatar asked Mar 30 '11 18:03

Evanss


People also ask

How does JQuery handle browser back button?

You can simply fire the “popState” event in JQuery e.g: $(window). on('popstate', function(event) { alert("pop"); });

How to prevent user from going back to previous page js?

Master, I have used the code that to prevent the user from going back to previous pages after logout. function preventBack() { window. history. forward(); } setTimeout("preventBack()", 0); window.

How to clear window history using JavaScript?

location. replace() it is used to clear the last entry in the history and replace it with the address of a new url. replace() removes the URL of the current document from the document history, meaning that it is not possible to use the "back" button to navigate back to the original document.


1 Answers

How about using document.referrer?

$(document).ready(function(){
    $('a.back').click(function(){
        if(document.referrer.indexOf(window.location.hostname) != -1){
            parent.history.back();
            return false;
        }
    });
});
like image 123
mattsven Avatar answered Oct 25 '22 17:10

mattsven