开发者

Where do you recommend loading in your jQuery library on WordPress site?

Where is the best place for pe开发者_运维百科rformance to load in the jQUery library on a WordPress site? header.php? footer.php? Thanks


YSlow recommendations would say the footer is the best place if you can manage it, because the JavaScript won't delay loading the page. Put all your JavaScript just before the </body> tag. Minify all of them and combine as many as possible to minimize downloads.


You can load jQuery in either place, but ideally you want to use WordPress' built-in function for adding scripts, including jQuery which they already have setup to do. It also allows you to include code in either the header or footer before the closing </body> tag.

http://codex.wordpress.org/Function_Reference/wp_enqueue_script

Example, put this in your functions.php file and it will de-register the currently included copy of jQuery in WordPress and include the latest version hosted by Google's CDN:

<?php
    function my_init_method() {
    wp_deregister_script( 'jquery' );
    wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js', false, '1.6', true);
    wp_enqueue_script( 'jquery' );
}    

add_action('init', 'my_init_method');
?>

EDIT: The code example above sets the last argument to "true" which will place jQuery in the footer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜