简体   繁体   English

检查是从主页还是从子页面重定向的方法

[英]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. 我尝试仅当用户从主页而不是子页面重定向时执行javascript动画。

I thought document.referrer.match() might do the thing, but I've probably messed up with match() parameters. 我以为document.referrer.match()可以做到这一点,但是我可能已经弄乱了match()参数。

Also, I'm running it on local server and iglakowe.com is the main page address. 另外,我也在本地服务器上运行它,而iglakowe.com是主页地址。

 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 这可能会返回类似https://stackoverflow.com/somePage.html的内容

strip the // from the returned string 从返回的字符串中删除//

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

ref will be https:stackoverflow.com/somePage.html 裁判将是https:stackoverflow.com/somePage.html

Now simply search for an occurence of the / char 现在只需搜索/ 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. ref现在是一个正整数,因为字符串内有一个/ ,这实际上意味着/跟随着文件夹或文件名之类的东西。

If it's -1 it's almost safe to say that the referer was the main page. 如果为-1,则几乎可以肯定地说引荐来源网址是主页。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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