0

I want to add IE8 support to my website but my website uses jquery v.2 that isn't supported by IE8. So i want to load jquery v.1 for users with IE8, but this doesn't work, can anyone see the mistake in my code?

When i visit the website in IE8 i get hundreds of errors because it is loading ONLY v.2 … . On the other hand safari is NOT loading v.1, so only IE has a problem here.

echo '<!--[if lt IE 9]>';
        wp_deregister_script( 'jquery' );
        wp_register_script( 'jquery', get_stylesheet_directory_uri() . "/_plugins/jquery/jquery-1.11.3.min.js", false, false, false );
        wp_enqueue_script( 'jquery' );
    echo '<![endif]-->';
    echo '<!--[if gte IE 9]><!-->';
        wp_deregister_script( 'jquery' );
        wp_register_script( 'jquery', get_stylesheet_directory_uri() . "/_plugins/jquery/jquery-2.1.4.min.js", false, false, false );
        wp_enqueue_script( 'jquery' );
    echo '<!--<![endif]-->';
herrfischer
  • 1,768
  • 2
  • 22
  • 35
  • Can you please copy and paste the errors that you are getting? – ham-sandwich Sep 13 '15 at 16:20
  • Also, please comment out that code, and copy and paste this into header.php and let me know if the error persists: https://gist.github.com/pjhampton/5af7f38a8b13787ee377 – ham-sandwich Sep 13 '15 at 16:21
  • I think you'll have a difficult time writing jQuery code that will support both. If you need legacy browser support then use the older version of jQuery. That said the reason you're having an issue is because of the way you're attempting to use enqueue script. It won't be shown inside your IE tags. – Nathan Dawson Sep 13 '15 at 16:24

1 Answers1

0

If you look at the generated script, you will just find this:

<!--[if lt IE 9]>
<![endif]-->
<!--[if gte IE 9]><!-->
<!--<![endif]-->

The scripts are only registered in the code, the scripts tags to load the scripts are written later. (If the scripts tags were written right away, then it would not be possible to deregister a script.)

Adapting a method from this question, you should be able to use this to wrap the script tags that are written:

wp_register_script('jquery1', get_stylesheet_directory_uri() . "/_plugins/jquery/jquery-1.11.3.min.js", false, false, false);
wp_register_script('jquery2', get_stylesheet_directory_uri() . "/_plugins/jquery/jquery-2.1.4.min.js", false, false, false);

add_filter('script_loader_tag', function($tag, $handle) {
  if ($handle === 'jquery1') {
    $tag = "<!--[if lt IE 9]>$tag<![endif]-->";
  }
  if ($handle === 'jquery2') {
    $tag = "<!--[if gte IE 9]><!-->$tag<!--<![endif]-->";
  }
  return $tag;
}, 10, 2);
Community
  • 1
  • 1
Guffa
  • 687,336
  • 108
  • 737
  • 1,005