Javascript 为什么我的脚本应该只在IE上启动,却在Mozilla上启动?
我下面的脚本是在Mozilla上启动的,而它应该只在IE上启动?它可以与Chrome一起正常工作。cookie会在每个浏览器会话中通知用户一次他们应该更新IE(如果是版本10或更低)。然而,Mozilla上的用户也得到了警告 代码:Javascript 为什么我的脚本应该只在IE上启动,却在Mozilla上启动?,javascript,jquery,cookies,Javascript,Jquery,Cookies,我下面的脚本是在Mozilla上启动的,而它应该只在IE上启动?它可以与Chrome一起正常工作。cookie会在每个浏览器会话中通知用户一次他们应该更新IE(如果是版本10或更低)。然而,Mozilla上的用户也得到了警告 代码: var key\u value=“Cookie=true”; var=0; //从该站点获取所有cookie并存储在一个数组中 var cookieArray=document.cookie.split(“;”); //遍历数组 for(var i=0;i
var key\u value=“Cookie=true”;
var=0;
//从该站点获取所有cookie并存储在一个数组中
var cookieArray=document.cookie.split(“;”);
//遍历数组
for(var i=0;i0){
返回parseInt(sAgent.substring(Idx+5,sAgent.indexOf(“.”,Idx));
}
//如果是IE 11,则查找更新的用户代理字符串。
else if(!!navigator.userAgent.match(/Trident\/7\./)){
返回11;
}
否则{
返回0;//它不是IE
}
}
修复了它。最后一个“返回0;”显然,返回了0,这使语句认为它是IE版本0:因此触发了警报。将此值更改为大于11的数字可以修复此值。您可能希望返回false
,并进行测试,或者使用第二个名为isIE()
的函数。这使得代码的作用更加明显。好主意。我会这样做,并测试它。谢谢。很有魅力,肯定比使用999要好得多。谢谢
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
}
}