最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Browser Back Button and Previous Page URL - Stack Overflow

programmeradmin9浏览0评论

I have a few questions related to the browser back button:

  1. How can I identify the URL of the previous page through which I have e to the current page?
  2. How can I identify that the request made is for the previous page in the browser history?
  3. 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:

  1. How can I identify the URL of the previous page through which I have e to the current page?
  2. How can I identify that the request made is for the previous page in the browser history?
  3. 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 badges
Add a ment  | 

2 Answers 2

Reset to default 4

1) 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

发布评论

评论列表(0)

  1. 暂无评论