I have a few questions related to the browser back button:
- How can I identify the URL of the previous page through which I have e to the current page?
- How can I identify that the request made is for the previous page in the browser history?
- How can I know that the user has clicked on the back button of the browser?
We have a requirement that when the user clicks on the back button in the application at some point in the application he is returned to an error page.
The implementation has to be through Javasctipt. I tried working with the history object but as of now I am stuck. Please help me.
I have a few questions related to the browser back button:
- How can I identify the URL of the previous page through which I have e to the current page?
- How can I identify that the request made is for the previous page in the browser history?
- How can I know that the user has clicked on the back button of the browser?
We have a requirement that when the user clicks on the back button in the application at some point in the application he is returned to an error page.
The implementation has to be through Javasctipt. I tried working with the history object but as of now I am stuck. Please help me.
Share Improve this question edited May 5, 2019 at 19:53 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Dec 22, 2011 at 6:31 Rishabh OhriRishabh Ohri 1,3104 gold badges16 silver badges29 bronze badges2 Answers
Reset to default 41) How can I identify the URL of the previous page through which I have e to the current page
window.document.referrer
return the url from where the page opened.
for other two questions, here is an example from MDN:
Example
Suppose http://mozilla/foo.html executes the following JavaScript:
var stateObj = { foo: "bar" };
history.pushState(stateObj, "page 2", "bar.html");
This will cause the URL bar to display http://mozilla/bar.html, but won't cause the browser to load bar.html or even check that bar.html exists.
Suppose now that the user now navigates to http://google., then clicks back. At this point, the URL bar will display http://mozilla/bar.html, and the page will get a popstate event whose state object contains a copy of stateObj. The page itself will look like foo.html, although the page might modify its contents during the popstate event.
If we click back again, the URL will change to http://mozilla/foo.html, and the document will get another popstate event, this time with a null state object. Here too, going back doesn't change the document's contents from what they were in the previous step, although the document might update its contents manually upon receiving the popstate event.
You can use document.referrer to see where they came from. Also a good history reference: https://developer.mozilla/en/DOM/Manipulating_the_browser_history