if condition: if the browser is IE and IE browser version is older than 9
The if condition below I think it says - if the browser is IE and IE browser version is newer than 9, but I don't have IE 9 to test it so it is hard to know the correct output, also this is not 100% of what I want bcos this script should be ran on other browsers too by default like Chrome, Firefox, etc - is it possible to set it in the if condition?
if ($.browser.msie && parseInt($.browser.version) > 9)
{
// run this code
}
The reason why I want to use if condition is that the script appears to have error on IE 7 and of course the best thing is to fix the script but I cannot tell which part of the script that IE doesn't accept it (all other browsers work perfectly fine!). Do u know any tool I can use to debug the script for IE 6, 7, 8, etc? I am using notepad++ to write my jquery, etc, so it doesn't provide any debug stuff...
So, my next best solution is not to run this script if it is IE browser which is older than 9.
By the way, this is the error message display on the IE7 browser but I can never understand it!
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开发者_JAVA技巧 or something else??
bloody IE!
thanks.
Your code looks fine, but you forgot to set the radix parameter in 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...");
}
This cannot cause any errors
Firstly, you can get IE9 preview by downloading it from Microsoft's site: http://ie.microsoft.com/testdrive/
Secondly, parseInt($.browser.version) > 9
would presumably check that the version is greater than 9, which of course it won't be until v10 is released. (you maybe intended >=
('greater than or equal to')?
I usually tell people to avoid browser detection or browser-specific code. There are times when it is necessary, but they're quite rare. Most of the time the developer would be better served by knowing what was failing and working around it (tools like Modernizr really help for this sort of thing).
However there are times when one simply has to do browser detection.
In your case, if you really need to detect IE, don't do it the way you're doing (ie checking for version 9); it'd be better to check for older versions, and I'd suggest that a conditional comment would be the best way to do it.
<script>
var i_am_old_ie = false;
<!--[if LT IE 9]>
i_am_old_ie = true;
<![endif]-->
</script>
Then your if()
statement later on can just look like this:
if(i_am_old_ie) {
//do stuff for IE6/7/8
} else {
//do stuff for all other browsers (including IE9)
}
Here is the Javascript version to test if the browser is IE:
<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>
This method is useful in case You are using HTML5 specific modules and want to implement some fallback functions in case the browser not supporting those features, for ex. <canvas>
There's an even easier method.
Since this is a comment, it will be ignored by all other browsers except IE.
var IE;
//@cc_on IE = navigator.appVersion;
Then just use this in you'r script to detect all IE versions.
if (IE) {
//This is IE.
}
else {
//This is NOT IE.
}
Or match against any version like this:
if (IE <= 6) {}, if (IE < 9) }, if (IE == 7) {} etc etc...
Here's the source where i found these types of conditionals: http://www.javascriptkit.com/javatutors/conditionalcompile.shtml
Instead of putting in a bunch of inline javascript you could just add an empty div for the version of ie you are targeting, so put the following at the top of your html, just inside the body tag:
<!--[if LT IE 9]>
<div id="iedetect" class="oldie"></div>
<![endif]-->
Then you can place an if statement in your jquery to check for that block.
if ($("#iedetect").is(".oldie")) {
//Do something
} else {
//Do Something else in all other browsers
}
<!--[if (IE 9)|!(IE)]><!--> Script here <!--<![endif]-->
I found cascading it works great for multibrowser detection. This code was used to change a fade to show/hide in ie 8 7 6.
$(document).ready(function(){
if(jQuery.browser.msie && jQuery.browser.version.substring(0, 1) == 8.0)
{
$(".glow").hide();
$('#shop').hover(function() {
$(".glow").show();
}, function() {
$(".glow").hide();
});
}
else
{ if(jQuery.browser.msie && jQuery.browser.version.substring(0, 1) == 7.0)
{
$(".glow").hide();
$('#shop').hover(function() {
$(".glow").show();
}, function() {
$(".glow").hide();
});
}
else
{if(jQuery.browser.msie && jQuery.browser.version.substring(0, 1) == 6.0)
{
$(".glow").hide();
$('#shop').hover(function() {
$(".glow").show();
}, function() {
$(".glow").hide();
});
}
else
{ $('#shop').hover(function() {
$(".glow").stop(true).fadeTo("400ms", 1);
}, function() {
$(".glow").stop(true).fadeTo("400ms", 0.2);});
}
}
}
});
<!--[if IE 8]>
<script type="text/javascript">
// this is only executed for IE 8
</script>
<![endif]-->
or you could do this
<!--[if IE 9]>
<script type="text/javascript">
// this is only executed for IE 9
</script>
<![endif]-->
精彩评论