Forums

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

  1. jahshuwaa
    Member
    Posted 2 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' />
    <![endif]-->

    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>
    <![endif]-->

    CODE TO ACHIEVE? (in functions.php of theme):
    global $wp_scripts;
    wp_register_script('html5shiv','http://html5shiv.googlecode.com/svn/trunk/html5.js',array(),'1.5.1');
    wp_enqueue_script('html5shiv');
    $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. jahshuwaa
    Member
    Posted 2 years ago #

    Still no progress on this. Is there a hook?

  3. jahshuwaa
    Member
    Posted 2 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>
    <![endif]-->
  5. Björn Ali Göransson
    Member
    Posted 1 year ago #

    Jahshuwaa,

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

  6. wmrom
    Member
    Posted 1 year 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:
    http://core.trac.wordpress.org/ticket/10891

    So it's on the radar.

  7. Mark Jaquith
    WordPress Lead Dev
    Posted 1 year 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:

    http://diveintohtml5.org/everything.html

  8. jahshuwaa
    Member
    Posted 1 year 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