Codesworth
Codesworth

Reputation: 115

jQuery: Shortening code to add an IE conditional?

I can't seem to add IE detection without repeating my code and changing a single line to allow it work with IE 8... hoverintent is the culprit in this instance, and doesn't work in IE8 > So I wanted detection to test for IE8 and simply use "hover" instead...

I have the conditional checker which works/detects ie8:

if (!$.support.leadingWhitespace) {
alert('This is IE8');
}

And this is my full code. For example on the second line you have .hoverIntent( ... which will need to change to just a simple .hover for MSIE8 ...

$(document).ready(function () {
     $('nav li,#mini-menu li').hoverIntent(

function (e) {
  //show its submenu
    e.stopPropagation();
  $('.sub-nav', this).stop(true,true).slideDown(200);
  $('.mini-nav', this).show();
}, 

function (e) {
  //hide its submenu
  e.stopPropagation();
    $('.sub-nav,.mini-nav', this).stop(true,true).fadeOut(100);         
}
 );

My problem is I've added the conditional checker code throughout the above, and the only way I guess I could do it is duplicating the above code and simply putting it within the conditional checker and changing .hoverintent to .hover.... So before I do, I would like to ask if there's a better way, how I can insert the conditional within the existing code and simply changing the line?

Upvotes: 1

Views: 187

Answers (1)

A. Wolff
A. Wolff

Reputation: 74420

You could do something like that:

var isIE = $.support.leadingWhitespace?false:true;

//then

$('nav li,#mini-menu li')[!isIE?'hoverIntent':'hover'](...);

Upvotes: 1

Related Questions