繁体   English   中英

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

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

我尝试仅当用户从主页而不是子页面重定向时执行javascript动画。

我以为document.referrer.match()可以做到这一点,但是我可能已经弄乱了match()参数。

另外,我也在本地服务器上运行它,而iglakowe.com是主页地址。

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

您可以通过一些字符串操作来做到这一点。 首先获取文档引荐来源

const ref = document.referrer;

这可能会返回类似https://stackoverflow.com/somePage.html的内容

从返回的字符串中删除//

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

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

现在只需搜索/ char的出现

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

ref现在是一个正整数,因为字符串内有一个/ ,这实际上意味着/跟随着文件夹或文件名之类的东西。

如果为-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