I think the best balance between efficiency, and using proper wordpress methods for adding javascript would be adding something along these lines to your themes functions.php file. For Example:
functions.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | function load_scripts() { global $post; if( is_page() || is_single() ) { switch($post->post_name) // post_name is the post slug which is more consistent for matching to here { case 'home': wp_enqueue_script('home', get_template_directory_uri() . '/js/home.js', array('jquery'), '', false); break; case 'about-page': wp_enqueue_script('about', get_template_directory_uri() . '/js/about-page.js', array('jquery'), '', true); break; case 'some-post': wp_enqueue_script('somepost', get_template_directory_uri() . '/js/somepost.js', array('jquery'), '1.6', true); break; } } } add_action('wp_enqueue_scripts', 'load_scripts'); |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.