Writing Conditional Code for Internet Explorer WITHIN a Javascript file

半腔热情 提交于 2019-12-05 19:55:40

If you are using jquery you code do this

if ($.browser.msie && $.browser.version == '6.0') {
     //do IE specific code
}

When writing Javascript, doing feature detection is always the way to go instead of browser detection. So instead of doing if (IE7) do if (feature).

For example, if you want to know if your browser supports getElementsByClassName(), instead of checking the browser version, you check for the existence of the function ( if (document.getElementsByClassName) ).

Please read this great article:

Object detection on Quirksmode

If you want to know whether the browser that views your page supports certain objects you want to use in your code, you should never EVER use a browser detect. Sure, you know that this–and–that browser will support your code while such–and–so browser won’t. But how about other browsers, obscure browsers?

Not within the JavaScript file directly.

A few alternatives would be:

Using a global variable before the script is loaded to check in your JavaScript file. This is a bit of a hybrid approach and could get messy, but it's guaranteed IE detection.

<!--[if IE]>
<script type="text/javascript">
  var is_ie = true;
</script>
<![endif]-->
<script type="text/javascript" src="somefile.js"></script>

Or, a more traditional approach using browser or object detection within the JavaScript file.

Conditional compilation is exactly what you are looking for.

<script>
/*@cc_on

  @if (@_jscript_version == 5.7 && window.XMLHttpRequest)
    document.write("You are using IE7");

  @end

@*/
</script>

My go-to script for this is PPK's BrowserDetect script. It's lightweight, easily understandable, and doesn't require you to use a library. When it's loaded, you can write code like:

if (BrowserDetect.browser == "Explorer" && BrowserDetect.version >= 6 && BrowserDetect.version <= 8) {
    // IE6-8 code
{

Of course, you should avoid using this at all (reasonable) costs, but there's times where it's cleaner to quarantine IE-specific code away rather than try to hack around IE-specific functions and bugs.

Tom Chantler

Despite the fact that this is an answer to the original question, this is NOT what you should do. So don't do it!

Why not work out which browser you are using and store that in a variable in javascript. Then you can have if statemenets and the like in your javascript. e.g. If I am IE then do this, otherwise do that. You get the idea!

Have you seen this? Browser sniffing

The salient bit:

var is = {
  ff: window.globalStorage,
  ie: document.all && !window.opera,
  ie6: !window.XMLHttpRequest,
  ie7: document.all && window.XMLHttpRequest && !XDomainRequest && !window.opera,
  ie8: document.documentMode==8,
  opera: Boolean(window.opera),
  chrome: Boolean(window.chrome),
  safari: window.getComputedStyle && !window.globalStorage && !window.opera
}

If you want to use jquery, it has a built in browser detect.

http://api.jquery.com/jQuery.browser/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!