4

Is it possible to use

var script = document.createElement('link');
script.href = 'theme/style/ie/manageleads.css';
script.rel  = 'stylesheet';
script.type = 'text/css';
document.getElementsByTagName('head')[0].appendChild(script);

and for it to only be active if the browser is IE8?

2 Answers 2

7

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

Note that IE8 claims to be 7 in Compatibility View.

if ($.browser == 'msie' && (parseInt($.browser.version) == 8 || parseInt($.browser.version) == 7))
{
    // Do something IE8-only in here
}
Sign up to request clarification or add additional context in comments.

2 Comments

"$.browser == 'msie'" did not work for me. instead I used "$.browser.msie" - worked like a charm. :)
@AlexZak Yes, jQuery changed the toString() of $.browser as it used to return the "name" of the browser. It's also worth nothing that as of jQuery 1.9, $.browser is removed, and no longer works - see the original link in my answer to jQuery.browser for more details
2

The best way would be conditional CSS, but since you asked for jQuery, here ya go.

if ($.browser.msie && parseInt($.browser.version) == 8) {
  $('<link />', {
    href: 'theme/style/ie/manageleads.css',
    rel: 'stylesheet',
    type: 'text/css'
  }).appendTo('head');
}

If that doesn't work, let me know. You might need to use 'document.createStylesheet' instead.

2 Comments

$.browser.version is likely to return something like 8.2256.4382, so you need to use (ideally) parseInt to just get the major version number back out of it.
Good call, I didn't notice that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.