开发者

Executing a js instruction depending on the user's browser?

开发者 https://www.devze.com 2023-02-18 18:19 出处:网络
I have a .js file with a instruction that is causing problems in IE. My question is if there is any way of making that instruction to execute in all browsers except in IE.

I have a .js file with a instruction that is causing problems in IE. My question is if there is any way of making that instruction to execute in all browsers except in IE.

I guess what I am lo开发者_开发百科oking for is something like conditional comments in HTML but for javascript. I know I could have two separate js files for each browser, but I would prefer a cleaner solution.

The solution can be plain js or JQuery.


This is a piece of cake with jQuery:

if ($.browser.msie) {
    // IE specific code.. You could even check for the version $.browser.version
} else {
    // Non-ie specific code
}

Hope this helped.


There are many ways, one:

if(!document.all || window.opera)
{
  //this will not be executed in msie
}


I've done something like this once to check for IE, specifically version 6:

var IEVersion = 0;  
if (navigator.appVersion.indexOf('MSIE') != -1)  
    IEVersion=parseFloat(navigator.appVersion.substr(navigator.appVersion.indexOf('MSIE') + 5, 3));
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号