1

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 "Liverpool fc" and open a website using open link in new tab

enter image description here

Now when the liverpool website opens in a new tab is there a way to go back to the google search?

enter image description here

The below function wont work:

function goBack() {
    window.history.back()
}

Is there any way out?

m0bi5
  • 8,900
  • 7
  • 33
  • 44

3 Answers3

0

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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

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

Ziv Ben-Or
  • 1,149
  • 6
  • 15
-1

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:

function OpenInNewTab(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);">Open last in new tab</a>
Community
  • 1
  • 1
Nick Schmidt
  • 1,427
  • 1
  • 13
  • 22
  • What's the purpose of your question? Perhaps there is another solution for your problem (I am asking, because normally no one needs something like this). – Nick Schmidt Feb 05 '15 at 12:27