Search code examples
javascriptbrowser-history

find out if last site was same as origin


I am using custom history manipulation on my onepage application (with history.pushState). As the user presses the browser native back button the site does not reload (thus not displaying the right content). I fixed this by listening for a window.onpopstate event where Id just reruning the initation script.

This however causes the user to be stuck on my page since as he tries to go back I simply reload the page, disreguarding if the page he wants to go to is on the same origin as mine or not. I would like somewhat like this:

window.onpopstate = () => {
  if (/*Site is same origin*/) load();
  else {
    //do Nothing
  }
};

How would I approch this?


Solution

  • You could approach this using document.referrer, match it with your domain and then take action accordingly. More about this at MDN.