Conditional Loading of Javascript-Files using WordPress (if IE)

Since version 4.2 it has become even more easy to load JavaScript files conditionally. Find out how this works in this blog post:

In your functions.php and/or in your plugin file you can use the follwoing function: wp_script_add_data():

<?php
add_action( 'wp_enqueue_scripts', 'ab_scripts' );

/**
 * Enqueue scripts and styles.
 *
function ab_scripts() {
	wp_enqueue_script( 'html5shiv', '//cdn.jsdelivr.net/html5shiv/3.7.2/html5shiv.js', array(), '3.7.2', false );
	wp_script_add_data( 'html5shiv', 'conditional', 'lt IE 9' );
}
?>

Check your sourcecode on the frontend and you will find something like this:

<!--[if lt IE 9]>
<script type='text/javascript' src='//cdn.jsdelivr.net/html5shiv/3.7.2/html5shiv.js'></script>
<![endif]-->

That’s it.