简体   繁体   中英

Way to check were you redirected either from a main page or subpage

I try to execute javascript animation only when a user gets redirected from the main page, not a subpage.

I thought document.referrer.match() might do the thing, but I've probably messed up with match() parameters.

Also, I'm running it on local server and iglakowe.com is the main page address.

 const ref = document.referrer.match(/iglakowe.com/) if (ref != null){ document.getElementById("bg").style.opacity = 0; } 

You can do this with some string manipulations. First get the document referrer

const ref = document.referrer;

This might return something like https://stackoverflow.com/somePage.html

strip the // from the returned string

const ref = document.referrer.replace("//","");

ref will be https:stackoverflow.com/somePage.html

Now simply search for an occurence of the / char

 const ref = document.referrer.replace("//","").indexOf("/");

ref is a positive integer now because there is a / inside the string which essentially means the / is following something like a folder or filename.

If it's -1 it's almost safe to say that the referer was the main page.

const ref = document.referrer.replace("//","").indexOf("/");
if (ref>-1){
 document.getElementById("bg").style.opacity = 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM