0

We are taught to place the following script in the <head> section, conditional to IE 9.

<!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->

As in WordPress I tend to use wp_enqueue_script() to hook scripts to wp_head() (default) or wp_footer(). My question is:

(1) can we implement something like the following in functions.php in WordPress theme?

function project_scripts(){?>
  <!--[if lt IE 9]>
  <?php
     wp_enqueue_script( 'IE-scripts', 'http://html5shiv.googlecode.com/svn/trunk/html5.js' );
  ?>
  <![endif]-->
<?php
}
add_action( 'wp_enqueue_scripts', 'project_scripts' );

(2) If so, can I do it otherwise:

function project_scripts(){
  echo '<!--[if lt IE 9]>';
     wp_enqueue_script( 'IE-scripts', 'http://html5shiv.googlecode.com/svn/trunk/html5.js' );
  echo '<![endif]-->';
}
add_action( 'wp_enqueue_scripts', 'project_scripts' );
Mayeenul Islam
  • 4,532
  • 5
  • 49
  • 102

1 Answers1

2

No. JavaScript cannot include IE conditionals.

This is because IE conditional are HTML comment nodes and, just as normal HTML markup makes no sense in JavaScript source, such comments result in invalid JavaScript.

It may be worthwhile to look into an approach like Modernizr which utilizes feature detection. Alternatively, IE conditional can be used to add classes to an element which can be detected later, as shown here.

Community
  • 1
  • 1
user2864740
  • 60,010
  • 15
  • 145
  • 220