enqueue javascript js with conditional comments (for IE) (8 posts)

  1. Josh S.
    Posted 5 years ago #

    Working in the theme's functions.php, how can I wp_enqueue_script a javascript file with conditional comments (for IE)?

    On the inclusion of CSS, wp_enqueue_style supports this for CSS with the following, for example:
    $wp_styles->add_data( 'theme-ie6', 'conditional', 'lt IE 7' );

    Results output:
    <!--[if lt IE 7]>
    <link rel='stylesheet' id='theme-ie6-css' href='/wp-content/themes/test/style_ie6.css?ver=1.0.0' type='text/css' media='all' />

    I thought the same techniques should work for wp_enqueue_script but I have not been successful.

    DESIRED RESULT on page/post/etc:
    <!--[if lt IE 9]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>

    CODE TO ACHIEVE? (in functions.php of theme):
    global $wp_scripts;
    $wp_scripts->add_data( 'html5shiv', 'conditional', 'lt IE 9' ); // DOES NOT WORK

    Need some expert help here to properly enqueue the scripts with conditional comments

  2. Josh S.
    Posted 5 years ago #

    Still no progress on this. Is there a hook?

  3. Josh S.
    Posted 5 years ago #

    Would a Mod please move this to the advanced forum?

  4. A simple if IE should do it.

    <!--[if IE]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  5. Björn Ali Göransson
    Posted 5 years ago #


    This feature is implemented in the styles part of wordpress as of now (even wordpress 3, latest build, doesn't have it).

  6. wmrom
    Posted 5 years ago #

    jahshuwaa: There doesn't seem to be a hook. I think this would be a good enhancement request for 3.1

    I looked on trac and found this:

    So it's on the radar.

  7. Mark Jaquith
    WordPress Lead Dev
    Posted 5 years ago #

    Also consider that you can use the global $is_IE to determine IE, and only enqueue if so. But if you want to not enqueue for IE 9 and higher, you'll have to do some User Agent parsing yourself.

    Also worth reading, on the subject of HTML5 support detection:


  8. Josh S.
    Posted 5 years ago #

    wmrom, good info. Thank you for the trac link.

    markjaquith, great suggestion and resource. I'll parse the user agent string in this case to enqueue conditionally.

Topic Closed

This topic has been closed to new replies.

About this Topic