我下面的脚本应该只在 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/