Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 363
mancestr Avatar asked Apr 09 '16 20:04

mancestr


People also ask

How can I tell if a page is redirected in JavaScript?

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

Can you check page source code of a redirected link?

You can't view the PHP source, even if it doesn't redirect you, the best you can see is the JavaScript source that the PHP code on the server sends you. If you want to see the PHP source on any site, you have to ask the owner for a copy or hack into the site.

Which property in JavaScript would you use to redirect visitor to another page?

With a few lines of JavaScript code, you can redirect visitors to another URL. The recommended function is window. location. replace() .


1 Answers

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

like image 63
Ramratan Gupta Avatar answered Sep 30 '22 16:09

Ramratan Gupta