Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 or something else??

bloody IE!

thanks.

like image 704
Run Avatar asked Nov 19 '10 15:11

Run


3 Answers

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

like image 83
Harmen Avatar answered Sep 19 '22 21:09

Harmen


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)
}
like image 32
Spudley Avatar answered Sep 20 '22 21:09

Spudley


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>

like image 29
Endre Simo Avatar answered Sep 19 '22 21:09

Endre Simo