2

I have a website which is running on Jquery 2.x. This is supported by IE 9+ and Chrome and Firefox. But I want the website to work with IE 8.

How can I unload Jquery 2.x and load 1.x for only IE 8 ?

I have tried using the following:

<!--[if lt IE 9]>
      <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>  
<![endif]-->

But both versions will load in this case.

Angelos Chalaris
  • 6,611
  • 8
  • 49
  • 75
Shahil M
  • 3,836
  • 4
  • 25
  • 44
  • 1
    Possible duplicate of [Loading different scripts versions depending on IE version](http://stackoverflow.com/questions/19315947/loading-different-scripts-versions-depending-on-ie-version) - You might need to combine a few instructions i.e: `[if lt IE 9]` with `[if IE 8]` etc.. Those should work and if both version are still loading you might still have a reference somewhere or the browser is using a chached version possibly. – Nope Mar 02 '17 at 11:39

2 Answers2

0

Try using a condition to check if the browser is IE8, like you did, and add a class to the <html> element:

<!--[if IE 8]> <html class="ie8"> <![endif]-->
<!--[if gt IE 8]><!--> <html> <!--<![endif]--> 

Then, in your Javascript code, you can easily check if the <html> element has the class specified for older browsers and load the script dynamically inside your page's <head>, like so:

var head = document.getElementsByTagName('head')[0];  
var script = document.createElement('script');
if(document.getElementsByTagName('html')[0].className == 'ie8')
    script.src = "jquery_1.x.js";
else
    script.src = "jquery_2.x.js";    
script.type = 'text/javascript';   
head.appendChild(script);

Note that this method might be quite slow, however.

Angelos Chalaris
  • 6,611
  • 8
  • 49
  • 75
0

You can add required jQuery version with following special conditional comments:

<!--[if lte IE 8]>
  <script src="/js/jquery-1.12.4.min.js"></script> 
<![endif]-->  
<!--[if (gte IE 9)]><!-->
  <script src="/js/jquery-3.3.1.min.js"></script>
<!--<![endif]-->

Note the additional <!--> just after the second if and its special closing <!--<![endif]-->. This will cause that modern browsers like as Chrome/FireFox don't consider it as comment and so include the new jQuery version.

S.Serpooshan
  • 7,608
  • 4
  • 33
  • 61