Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
657 views
in Technique[技术] by (71.8m points)

redirect - Detect if page was redirected or loaded directly(Javascript)

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...