从CDN加载jQuery,回退到本地(对于Wordpress)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从CDN加载jQuery,回退到本地(对于Wordpress)相关的知识,希望对你有一定的参考价值。
Add the following to your Wordpress theme's functions.php file. The jQuery paths/URLs might need to be changed to suite your circumstances.
/** * Load jQuery from Google CDN, fallback to local */ // http://wp.tutsplus.com/tutorials/load-jquery-from-google-cdn-with-local-fallback-for-wordpress/ if( !is_admin()){ // Don't do this for admin area, since Google's jQuery isn't in noConflict mode and will interfere with WP's admin area. $url = 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js'; // the URL to check against if($test_url !== false) { // test if the URL exists function load_external_jQuery() { // load external file wp_deregister_script( 'jquery' ); // deregisters the default WordPress jQuery wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js'); // register the external file wp_enqueue_script('jquery'); // enqueue the external file } add_action('wp_enqueue_scripts', 'load_external_jQuery'); // initiate the function } else { function load_local_jQuery() { wp_deregister_script('jquery'); // deregisters the default WordPress jQuery wp_register_script('jquery', get_bloginfo('template_url').'/js/jquery-1.6.2.min.js', __FILE__, false, '1.6.2', true); // register the local file wp_enqueue_script('jquery'); // enqueue the local file } add_action('wp_enqueue_scripts', 'load_local_jQuery'); // initiate the function } }
以上是关于从CDN加载jQuery,回退到本地(对于Wordpress)的主要内容,如果未能解决你的问题,请参考以下文章
JavaScript 从Google CDN加载jQuery,但如果失败则回退到本地
HTML 正确的方法从谷歌的CDN加载jQuery,必要时回退到本地库
从Google的CDN加载jQuery的正确方法,如果需要,可以使用回退到本地库