WordPress - Enqueue scripts for only if LT IE 9

前端 未结 7 760
无人及你
无人及你 2021-01-31 09:13

In a WordPress theme, how do you conditionally include scripts for ie8 and below? This is primarily to apply polyfills for various html5/css3 features. I see that wp has the $is

7条回答
  •  南旧
    南旧 (楼主)
    2021-01-31 09:45

    • You should use wp_register_script() whenever you add scripts to your WP themes or plugins.
    • Server side sniffing is generaly bad idea, because it's unsure.
    • Usually you want to target browsers lacking sertain features, not necessarily only IE browser. Modernzr is good scripts to do just that.

    For IE, conditional tags work very well. Here's how you can add conditional tags into a script, example is for HTML5shiv:

    global $wp_scripts;
    wp_register_script( 
        'html5shiv', 
        get_bloginfo('template_url').'/assets/js/html5shiv.js', 
        array(), 
        '3.6.2'
        );
    $wp_scripts->add_data( 'html5shiv', 'conditional', 'lt IE 9' );
    

提交回复
热议问题