In JavaScript world, if I click on a button on page A and it opens up a new tab (calling it page B), is it possible to get page A's browsing history from page B? In my case there isn't a way to make changes on page A, so sending data via query string or something like that is not doable. All I'm needing is to get the url that page B comes from. Thanks!
-
Definitely not if they are in different domains... I doubt even if they are. Since you say "page A" is not available, I'm guessing there isn't much you can do. You *might* be able to get the last URL via [`document.referrer`](https://developer.mozilla.org/en-US/docs/Web/API/Document/referrer). Do note that if you don't trust the owner of "page A", you shouldn't trust that value; it is trivial to spoof. – Heretic Monkey May 19 '17 at 21:42
3 Answers
If I understand your question correctly and you want the url of the page A that sent you to page B then you can get the referrer to the site with document.referrer.split( '/' );
.
If you want to, as your phrasing may imply, access the history of the browser that is not possible via common Javascript.

- 1,112
- 11
- 26
You can use the back()
method in javascript.
function getHistory(){
var $history = window.history.back();
window.open($history, '_blank');
window.focus();
}
<input type="button" value="Click me" onclick="getHistory();">
Edit: NOTE: Opening new tab is not that easy. You can read this

- 1
- 1

- 840
- 13
- 30
Seems like you could achieve this via document.referrer, which will give you the URL of the site that loaded the current document:
https://www.w3schools.com/jsref/prop_doc_referrer.asp
I'm by far no JavaScript expert, but from what I've read it isn't possible to get the history of the browser itself and the history api like history.back() and history.forward() only allow you to navigate forward and backwards.

- 433
- 1
- 4
- 12