Getting Last Page Url From History Object - Cross Browser?
Solution 1:
You cant get to history in any browser. That would be a serious security violation since that would mean that anyone can snoop around the history of their users.
You might be able to write a Browser Helper Object for IE and other browsers that give you access to that. (Similar to the google toolbar et al). But that will require the users to allow that application to run on their machine.
There are some nasty ways you can get to some history using some "not-so-nice" ways but I would not recommend them. Look up this link.
Solution 2:
Of course, as people have said, its not possible. However what I've done in order to get around this limitation is just to store every page loaded into localStorage so you can create your own history ...
functionwriteMyBrowserHistory(historyLength=3) {
// Store last historyLength page paths for use in other pagesvar pagesArr = localStorage.myPageHistoryif (pagesArr===null) {
pagesArr = [];
} else {
pagesArr = JSON.parse(localStorage.myPageHistory);
pagesArr.push(window.location.pathname) // can use whichever part, but full url needs encoding
}
if (pagesArr.length>historyLength) {
// truncate the array
pagesArr = pagesArr.slice(pagesArr.length-historyLength,pagesArr.length)
}
// store it back localStorage.myPageHistory = JSON.stringify(pagesArr);
// optional debugconsole.log(`my page history = ${pagesArr}`)
}
functiongetLastMyBrowserHistoryUrl() {
var pagesArr = localStorage.myPageHistoryvar url = ""if (pagesArr!==null) {
pagesArr = JSON.parse(localStorage.myPageHistory);
// pop off the most recent url
url = pagesArr.pop()
}
return url
}
So then on a js in every page call
writeMyBrowserHistory()
When you wanna figure out the last page call
var lastPageUrl = getLastMyBrowserHistoryUrl()
Note: localStorage stores strings only hence the JSON.
Let me know if I have any bugs in the code as its been beautified from the original.
Post a Comment for "Getting Last Page Url From History Object - Cross Browser?"