不确定document.referrer是否正常工作

时间:2015-01-07 17:49:33

标签: 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/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是我正在寻找的确切网址,并且在最后使用反斜杠并且没有反复使用它。

1 个答案:

答案 0 :(得分:-2)

这是您的解决方案。必须更改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