lau*_*kok 11 jquery internet-explorer if-statement internet-explorer-7
我认为它下面的if条件 - 如果浏览器是IE和IE浏览器版本比9更新,但我没有IE 9来测试它所以很难知道正确的输出,这也不是100%我希望bcos这个脚本应该在其他浏览器上默认运行,如Chrome,Firefox等 - 是否可以在if条件下设置它?
if ($.browser.msie && parseInt($.browser.version) > 9)
{
// run this code
}
Run Code Online (Sandbox Code Playgroud)
我想使用if条件的原因是脚本似乎在IE 7上有错误,当然最好的办法是修复脚本,但我不知道IE的哪个部分不接受它(所有其他浏览器工作得很好!).你知道我可以用来调试IE 6,7,8等脚本的工具吗?我使用notepad ++编写我的jquery等,所以它不提供任何调试内容......
所以,我的下一个最佳解决方案是,如果它是早于9的IE浏览器,则不运行此脚本.
顺便说一句,这是IE7浏览器上显示的错误信息,但我永远无法理解!
Line:910 //which line?
Char:4 // what the hell is this?
Error: Object doesn't support this property or method //what?
Code: 0 // 0 of what?
URL: http://localhost/mysite/page-1 // so which file is causing the error then? the .js or .html or something else??
Run Code Online (Sandbox Code Playgroud)
血淋淋的IE!
谢谢.
Har*_*men 12
您的代码看起来很好,但您忘记在以下位置设置radix参数parseInt:
if ($.browser.msie && parseInt($.browser.version, 10) > 9){
// you need to wait a couple years to test if it works...
alert("I'm IE10 or 11...");
}
Run Code Online (Sandbox Code Playgroud)
这不会导致任何错误
Spu*_*ley 10
首先,您可以从Microsoft的网站下载IE9预览版:http://ie.microsoft.com/testdrive/
其次,parseInt($.browser.version) > 9可能会检查版本是否大于 9,这当然不会发布v10.(你可能打算>=('大于或等于')?
我经常告诉人们避免浏览器检测或浏览器特定的代码.有时候有必要,但它们很少见.大多数情况下,开发人员可以通过了解失败和解决问题得到更好的服务(像Modernizr这样的工具真的有助于此类事情).
然而,有时候只需要进行浏览器检测.
在您的情况下,如果您确实需要检测IE,请不要按照您的方式执行(即检查版本9); 检查旧版本会更好,我建议有条件的评论是最好的方法.
<script>
var i_am_old_ie = false;
<!--[if LT IE 9]>
i_am_old_ie = true;
<![endif]-->
</script>
Run Code Online (Sandbox Code Playgroud)
那么你if()以后的陈述可能看起来像这样:
if(i_am_old_ie) {
//do stuff for IE6/7/8
} else {
//do stuff for all other browsers (including IE9)
}
Run Code Online (Sandbox Code Playgroud)
这是测试浏览器是IE的Javascript版本:
<script>
function getIEVersion() {
var rv = -1; // Return value assumes failure.
if (navigator.appName == 'Microsoft Internet Explorer') {
var ua = navigator.userAgent;
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if (re.test(ua) != null)
rv = parseFloat( RegExp.$1 );
}
return rv;
}
function checkVersion() {
var ver = getIEVersion();
if ( ver != -1 ) {
if (ver <= 9.0) {
// do something
}
}
}
checkVersion();
</script>
Run Code Online (Sandbox Code Playgroud)
如果您正在使用HTML5特定模块并希望在浏览器不支持这些功能的情况下实现某些回退功能,则此方法很有用,例如. <canvas>
有一种更简单的方法.
由于这是一个注释,除IE之外的所有其他浏览器都会忽略它.
var IE;
//@cc_on IE = navigator.appVersion;
Run Code Online (Sandbox Code Playgroud)
然后在您的脚本中使用它来检测所有IE版本.
if (IE) {
//This is IE.
}
else {
//This is NOT IE.
}
Run Code Online (Sandbox Code Playgroud)
或者匹配任何这样的版本:
if (IE <= 6) {}, if (IE < 9) }, if (IE == 7) {} etc etc...
Run Code Online (Sandbox Code Playgroud)
以下是我找到这些类型条件的来源:http: //www.javascriptkit.com/javatutors/conditionalcompile.shtml