javascriptinternet-explorerconditional-commentslabjs

LABjs Loading IE Conditions


I'm having an issue with LABjs. I load all my scripts, but when I use it in IE it completely breaks down.

 $LAB
     .script('https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js').wait()
     .script('https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js')

And if I add a conditional statement

<!--[if IE ]>
     <script type="text/javascript" src="somescript.js"></script>
<![endif]-->

This would fail. How would I include this into LABjs?

Thanks.


Solution

  • I would first STRONGLY say, you shouldn't be doing IE-only scripts any more. But if you really absolutely must, and after you slap yourself on the wrist several times for that bad practice...

    (function(){
    
       // From: http://james.padolsey.com/javascript/detect-ie-in-js-using-conditional-comments/
       var ie_version=(function(){var c,a=3,b=document.createElement('div'),d=b.getElementsByTagName('i');while(b.innerHTML='<!--[if gt IE '+(++a)+']><i></i><![endif]-->',d[0]);return a>4?a:c})();
    
       $LAB.setOptions({AlwaysPreserveOrder:true})
       .script('https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js')
       .script('https:///ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js')
       .script(ie_version ? 'somescript.js' : '')
    })();
    

    The ie_version snippet of code comes from: http://james.padolsey.com/javascript/detect-ie-in-js-using-conditional-comments/ which uses dynamically created IE conditional comments to determine an IE version.

    NOTE: I'm assuming that "somescript.js" needs to be executed strictly after the jquery and jquery-ui scripts, based on what you posted. If that's not the case, you could put it in a different position in the $LAB chain if you wanted to, and you could take out the setOptions({AlwaysPreserveOrder:true}) and instead use wait() in the chain only where desired/necessary.