Detect if page was redirected or loaded directly(Javascript)

醉酒当歌 提交于 2020-08-27 19:33:31

问题


Currently, I want to display a label on the first page of my site a user visits.

To make it simple, say I only care about page1.html and page2.html.

Is it possible to check if the user was redirected from page1.html to page2.html?(Perhaps with cookies?)

Edit: On page1.html, I set a cookie, which would expire in a minute. On page2.html, I checked for the cookie. Works great!


回答1:


You can use document.referrer.

The value is an empty string if the user navigated to the page directly (not through a link).

  1. For example when user come to URL via bookmaked links click.
  2. User type the full URL in address bar.

Reference https://developer.mozilla.org/en-US/docs/Web/API/Document/referrer



来源:https://stackoverflow.com/questions/36522178/detect-if-page-was-redirected-or-loaded-directlyjavascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!