Javascript 不确定document.referer是否工作
我有一些简单的JS,可以根据一些条件将用户重定向到页面之外Javascript 不确定document.referer是否工作,javascript,html,redirect,Javascript,Html,Redirect,我有一些简单的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/tab
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,我尝试过在末尾加反斜杠和不加反斜杠。这是您的解决方案。只需更改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");
}
}
请参阅我的。“我不知道lastPage得到了什么值”-然后禁用重定向,并将该值记录到console–噢!更改&&lastPage!="http://www.com.edu/“
至&&lastPage&&lastPage!="http://www.com.edu/“
;有时,由于各种原因,推荐人不在那里。依赖文档是个坏主意。推荐人是任何东西。不要认为在chrome中测试一次就足够了。有些浏览器甚至实现了不同的referer拼写(我不是开玩笑的),有些浏览器在文档中没有,有些浏览器默认阻止它,有些浏览器基于域/协议/内容类型阻止它……如果您使用chrome开发工具(或类似工具),您可以设置断点并逐步完成代码。在您的场景中,它应该非常明显