DBroncos1558 DBroncos1558 - 4 months ago 17
jQuery Question

Running a javascript IF internet explorer is detected

I have this code:

$(document).ready(function () {
$('body a[href]').qtip({
hide: {
fixed: true,
delay: 500
},
style: {
classes: 'qtip-dark qtip-shadow'
},
position: {
viewport: $(window)
}
});
jQuery.each(jQuery.browser, function (i, val) {
$("<div>" + i + " : <span>" + val + "</span>")
.appendTo(document.body);
});
});


In addition to the above code, how would I run a script ONLY if internet explorer is detected from this browser function?
The script i want to run if internet explorer is detected is:

$(document).ready(function () {
$('body a[href]').qtip({
hide: {
fixed: true,
delay: 500
},
style: {
classes: 'qtip-dark qtip-shadow'
}
});

Answer

Use IE conditional comments.

<!--[if IE]>
<script>
// run code here or link to external file
</script>
<![endif]-->

They're highly customizable, too -- <!--[if lt IE 8]> will only run for IE 7 or below, for instance. Developers use this to create custom stylesheets for IE6/7/8 all the time.

That said, you should really consider using feature detection instead of browser detection for whatever you're trying to implement. See http://modernizr.com for what is arguably the best solution yet devised.