Javascript条件浏览器检测会干扰其他功能

时间:2013-02-19 12:43:18

标签: javascript browser-detection

出于某种原因,这部分javascript阻止了我之前编写的所有其他功能。如果我把它拿出来就可以工作,当我把它拿回来时就停止工作。

如果有人能发现错误,请告诉我!我真的不太喜欢JS,所以我不太确定我在这里做什么。

好吧,我用这个作为参考; http://www.javascriptkit.com/javatutors/navigator.shtml

这是我的代码:

//4th batch shows a warning box upon finding a visitor with an outdated browser
if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ //test for Firefox/x.x or Firefox x.x (ignoring remaining digits);
 var ffversion=new Number(RegExp.$1) // capture x.x portion and store as a number
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
 var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
if (/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ //test for Opera/x.x or Opera x.x (ignoring remaining decimal places);
 var oprversion=new Number(RegExp.$1) // capture x.x portion and store as a number

if (ffversion<11 || ieversion<9 || oprversion<11.6)
 function browserwarning()
 {
 document.getElementById('oldbrowser').style.display="inherit";
 }
 //END OF 4TH BATCH

2 个答案:

答案 0 :(得分:0)

有很多{没有相应的}

if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)) { 
    var ffversion = new Number(RegExp.$1);
}
else if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
    var ieversion = new Number(RegExp.$1);
}
else if (/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent)) { 
    var oprversion=new Number(RegExp.$1);
}

if (ffversion < 11 || ieversion < 9 || oprversion < 11.6) {
       document.getElementById('oldbrowser').style.display="inherit";
}

我刚刚关闭了所有括号。

无论如何,我建议你抓住这个:http://www.quirksmode.org/js/detect.html

答案 1 :(得分:0)

你忘记了if-statesment无效的一些右括号和函数。此代码可以在没有语法错误的情况下运行:

//4th batch shows a warning box upon finding a visitor with an outdated browser
if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)) { //test for Firefox/x.x or Firefox x.x (ignoring remaining digits);
    var ffversion = new Number(RegExp.$1); // capture x.x portion and store as a number
} else if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { //test for MSIE x.x;
    var ieversion = new Number(RegExp.$1); // capture x.x portion and store as a number
} else if (/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent)) { //test for Opera/x.x or Opera x.x (ignoring remaining decimal places);
    var oprversion = new Number(RegExp.$1); // capture x.x portion and store as a number
}

if (ffversion < 11 || ieversion < 9 || oprversion < 11.6) {
    document.getElementById('oldbrowser').style.display = "inherit";
}

但我强烈建议您学习如何使用和编写JavaScript代码。看起来你正试着用中文写一封信,虽然你甚至不知道他们的符号......