• Josh S.

    (@jahshuwaa)


    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&gt;
    <![endif]–>

    CODE TO ACHIEVE? (in functions.php of theme):
    global $wp_scripts;
    wp_register_script(‘html5shiv’,’http://html5shiv.googlecode.com/svn/trunk/html5.js&#8217;,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

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter Josh S.

    (@jahshuwaa)

    Still no progress on this. Is there a hook?

    Thread Starter Josh S.

    (@jahshuwaa)

    Would a Mod please move this to the advanced forum?

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Advisor and Activist

    A simple if IE should do it.

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

    Jahshuwaa,

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

    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.ww.wp.xz.cn/ticket/10891

    So it’s on the radar.

    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

    Thread Starter Josh S.

    (@jahshuwaa)

    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.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘enqueue javascript js with conditional comments (for IE)’ is closed to new replies.