javascript - 为什么我的脚本应该只在 IE 上触发,却在 Mozilla 上触发?

标签 javascript jquery cookies

我下面的脚本应该只在 IE 上触发,但它却在 Mozilla 上触发?它与 Chrome 一起正常工作。 cookie 在每个浏览器 session 中通知用户一次他们应该更新 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
    }
}

最佳答案

修复它。最后'return 0;'很明显,它返回了 0,这使得该语句认为它是 IE 版本 0:因此触发了警报。将其更改为大于 11 的数字可以修复它。

关于javascript - 为什么我的脚本应该只在 IE 上触发,却在 Mozilla 上触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28961232/

相关文章:

javascript - : How To Have Ajax Validation On A Part Of The Form Showed With An Ajax Event And A Renderpartial? yii

jquery - 如何在提交具有大型 Base64 编码图像的表单时显示百分比进度条

javascript - 点击<a href>标签,一个div向右滑动打开,如果再次点击<a href>标签,div向左关闭?

python - 使用 Mechanize 接受和发送 Cookie

java - GWT Cookie 在 Firefox 中未被删除

android - 在 CookieSyncManager.createInstance 中使用应用程序或 Activity 上下文

javascript - 是否有任何简写方法将以标题作为第一个数组的字符串数组转换为数组对象?

javascript - 如何为 JSON 创建错误处理函数

javascript - 悬停时颜色变为灰度在 IE11 中不起作用

javascript - Protractor 获取元素的比例或缩放宽度和高度