为什么我的脚本只能在IE上触发时才会触发Mozilla?

时间:2015-03-10 10:26:34

标签: javascript jquery cookies

下面我的脚本是在Mozilla上启动的时候它应该只在IE上启动?它适用于Chrome。 Cookie会在每个浏览器会话中通知用户他们应该更新IE(如果是10或更低版本)。但是,Mozilla上的用户也会收到警报。

代码:

var key_value = "Cookie=true";
var foundCookie = 0;

// Get all the cookies from this site and store in an array
var cookieArray = document.cookie.split(';');

// Walk through the array
for(var i=0;i < cookieArray.length;i++)
{
    var checkCookie = cookieArray[i];
    // Remove any leading spaces
    while (checkCookie.charAt(0)==' ')
    {
        checkCookie = checkCookie.substring(1,checkCookie.length);
    }

    // Look for cookie set by key_value
    if (checkCookie.indexOf(key_value) == 0)
    {
        // alert("Found Cookie");
        // The cookie was found so set the variable
        foundCookie = 1;
    }
}
// Check if a cookie has been found
if ( foundCookie == 0)
{
    // The key_value cookie was not found so set it now
    document.cookie = key_value;
    if (GetIEVersion() < 11) {
    alert("You are using an outdated version of Internet Explorer.");
}


}

function GetIEVersion() {
    var sAgent = window.navigator.userAgent;
    var Idx = sAgent.indexOf("MSIE");

    // If IE, return version number.
    if (Idx > 0) {
        return parseInt(sAgent.substring(Idx + 5, sAgent.indexOf(".", Idx)));
    }
    // If IE 11 then look for Updated user agent string.
    else if (!!navigator.userAgent.match(/Trident\/7\./)) {
        return 11;
    }

    else {
        return 0; //It is not IE
    }
}

1 个答案:

答案 0 :(得分:2)

修正了它。最后一次返回0;&#39;显然,它返回0,这使得该声明认为它是IE版本0:因此触发警报。将此值更改为高于11的数字会修复它。