我有一些简单的JS,用于根据一些条件将用户从页面重定向。
var lastPage = document.referrer;
var currentPage = window.location.href;
var myElem = document.getElementById('topframe.login.label');
if (currentPage == "https://de.com.edu/webapps/portal/execute/tabs/tabAction?tab_tab_group_id=_1_1")
{
if (myElem != null && lastPage != "http://www.com.edu/")
{
window.location.replace("https://de.com.edu/webapps/login/?action=relogin");
}
}
基本上,如果用户最终出现在第一个链接上,请检查currentPage
我想看看他们是否来自地址http://www.com.edu
,如果没有,则使用 window.location.replace
重定向他们。
问题是这段代码似乎无论如何都有效(例如,用户总是被重定向)。我不知道lastPage
获得了什么价值,并且在Chrome的开发人员控制台中没有看到任何错误。我做错了什么愚蠢的事情吗?我已经确认http://www.com.edu
是我正在寻找的确切URL,并且已经尝试了最后带有反斜杠和没有反斜杠的URL。
这是您的解决方案。只需要改变null
完整代码:
var lastPage = document.referrer;
var currentPage = window.location.href;
var myElem = document.getElementById('topframe.login.label');
if (currentPage == "https://de.com.edu/webapps/portal/execute/tabs/tabAction?tab_tab_group_id=_1_1")
{
if (myElem != '' && lastPage != "http://www.com.edu/")
{
window.location.replace("https://de.com.edu/webapps/login/?action=relogin");
}
}
请参阅我的 JSFIDDLE。