Site icon WP Smith

Javascript (JS) to Footer Except…

YSlow! recommends that you move Javascript to the footer. However, not all cases lend itself to move one's JS to the footer. So here is a quick and easy way to do this in functions.php. For one of my sites running with Shopp, I needed the javascript (js) to run in the header as running it in the footer disabled the buttons. So here is what I did to fix my Shopp problem with Views buttons.

[php]//JS in header for Shopp else in footer
$pages = array('store','account','checkout','cart'); //these are the 4 pages created by default by Shopp
if (is_page(!$pages)) {
remove_action('wp_head', 'wp_print_scripts');
remove_action('wp_head', 'wp_print_head_scripts', 9);
remove_action('wp_head', 'wp_enqueue_scripts', 1);
add_action('wp_footer', 'wp_print_scripts', 5);
add_action('wp_footer', 'wp_enqueue_scripts', 5);
add_action('wp_footer', 'wp_print_head_scripts', 5);
}
[/php]

So, if it is not one of my Shopp pages, then I run the scripts in my footer.