在IE8中破坏了document.referrer.indexOf()

时间:2013-05-23 22:22:48

标签: javascript internet-explorer internet-explorer-8

以下函数在IE8中的第一个if语句处断开。我不确定是什么会导致这种情况,因为从我所研究的内容来看,这一切都不应该引起问题。我也尝试在toLowerCase()属性之后添加referrer方法,但仍然没有运气。有什么想法吗?

function returnToLogin() {
    if (document.referrer.indexOf('attendant_login') > 0) {
        if (thisevent == null) {
            window.location = document.referrer;
        } else {
            setTimeout(returnToLogin, 1000);
        }
        return true;
    }
    return false;
}

3 个答案:

答案 0 :(得分:2)

IE并不总是设置document.referrer属性。解决方案是在调用方法之前检查它是否已定义。将您的if更改为:

if (document.referrer&&document.referrer.indexOf('attendant_login') > 0) {

现在如果document.referrer不存在,它将不会尝试在其上调用indexOf方法,因此它不会中断。相反,它只会表现得好像测试失败(我认为这是一个合适的默认值)

答案 1 :(得分:1)

您使用的

document.referrer将返回除Internet Explorer之外的所有浏览器的请求页面URL,在some cases中它实际上在IE中返回null。

答案 2 :(得分:-1)

IE在使用Array.indexOf()时遇到问题。请改用jQuery.inArray()

相关问题