Skip to content Skip to sidebar Skip to footer

Javascript : History Back() Method

I was going through the history back() method in Javascript at W3School's website. I was wondering if its possible to go back in history in a new tab. Lets say I google search 'Liv

Solution 1:

No, it isn't.

That page isn't part of the current window's history.

That is why the browser's back button wouldn't work either.

Solution 2:

You can send the url(window.location.href) to the new tab and in the new tab use the history api to push the url to the history state. Look here: Working with the History API

Solution 3:

Edit: Misundersood your question.

So, if you want to create this on you own, it is possible to give the url you are opening in the new tab an attribute with the referrer url. Something like this:

http://yourpage.com/?referrer=http%3A%2F%2Fyourpage.com%252Fsublink

Otherwise there is no possiblity for what you want to achieve.


THIS IS NOT THE SOLUTION, JUST DOESN'T WANT TO WASTE IT

It actually is possible. Just take a look at the document object, and you will find a referrer attribute. It's the URL you are coming from.

If you want to open a new tab you should take the workaround from duke that looks like this:

functionOpenInNewTab(url) {
  var win = window.open(url, '_blank');
  win.focus();
}

After this, you can create a new link with an onclick attribute:

<a onclick="OpenInNewTab(document.referrer);">Openlastinnew tab</a>

Post a Comment for "Javascript : History Back() Method"