Domanda

I would like to trigger a javascript event for ie8 or lower versions. This is my markup:

<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie10 lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie10 lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie10 lt-ie9"> <![endif]-->
<!--[if IE 9]>         <html class="no-js lt-ie10"> <![endif]-->
<!--[if gt IE 9]><!--> <html class="no-js"> <!--<![endif]-->
...
</html>

And jquery:

$( document ).ready(function() {
    $("html.lt-ie9").load(function(){
        alert('ie8 only');
    });
});

But this is doing nothing at all in any browser.

What is the correct way to do this?

È stato utile?

Soluzione

On document ready you could just check if the html element has that certain IE class and then do something:

$( document ).ready(function() {
    if ($('html').hasClass('lt-ie9')) {
      alert('ie8 only');
    }
});

Altri suggerimenti

You might not want to do this, like this. But it should work.

$( document ).ready(function() {
   if($('html').hasClass('lt-ie9')) {
      // later than IE9 only...
   }
});

I would recommend that you used feature detection instead of browser detection .

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top