使用“后备脚本”从Wordpress中的Google AJAX Libraries API加载jQuery [英] Load jQuery from Google AJAX Libraries API in Wordpress with "fallback script"

查看:105
本文介绍了使用“后备脚本”从Wordpress中的Google AJAX Libraries API加载jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  if(!is_admin())add_action() wp_enqueue_scripts,my_jquery_enqueue,11); 
函数my_jquery_enqueue(){
wp_deregister_script('jquery');
wp_register_script('jquery',http。($ _SERVER ['SERVER_PORT'] == 443?s:)。://ajax.googleapis.com/ajax/libs/jquery/ 1.9.1 / jquery.min.js,false,null);
wp_enqueue_script('jquery');
}

它创建了这样的内容

 < script src =// ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js\"> ;</script> ; 

如何使用fallback选项包含它,如html5boilerplate建议的那样

 < script src =// ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js\"> ;< /脚本> 
< script> window.jQuery || document.write('< script src =js / vendor / jquery-1.9.1.min.js>< \ / script>')< / script>

像这样

解决方案

试试这个。
用此代码替换您的代码。这将处理所有。

  / ************* ENQUEUE JS ***** ******************** / 

/ *从谷歌的CDN中取出jquery。如果不可用,请抓取本地副本。 * /

$ url ='http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'; //要检查的
$ test_url = @fopen($ url,'r'); ($ test_url!== false){//测试URL是否存在

函数load_external_jQuery() jquery'); //注销默认的WordPress jQuery
wp_register_script('jquery','http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'); //注册外部文件
wp_enqueue_script('jquery'); //排队外部文件

$ b $ add_action('wp_enqueue_scripts','load_external_jQuery'); //启动函数
} else {

函数load_local_jQuery(){
wp_deregister_script('jquery'); //启动函数
wp_register_script('jquery',get_template_directory_uri()。'/ js / jquery.js',__FILE__,false,'1.7.2',true); //注册本地文件

wp_enqueue_script('jquery'); //排队本地文件
}

add_action('wp_enqueue_scripts','load_local_jQuery'); //启动函数
}

将这段代码放在functions.php中


I have added a Wordpress function to load jQuery Library from Google Hosted Libraries

if (!is_admin()) add_action("wp_enqueue_scripts", "my_jquery_enqueue", 11);
function my_jquery_enqueue() {
   wp_deregister_script('jquery');
   wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js", false, null);
   wp_enqueue_script('jquery');
}

it create the something like this

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

how can I include it with fallback option, as per html5boilerplate suggested

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.9.1.min.js"><\/script>')</script>

like this

解决方案

Try this. Replace your code with this code. This will handle all.

/************* ENQUEUE JS *************************/

/* pull jquery from google's CDN. If it's not available, grab the local copy. */

$url = 'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'; // the URL to check against  
$test_url = @fopen($url,'r'); // test parameters  
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.9.1/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'); // initiate the function  
        wp_register_script('jquery', get_template_directory_uri().'/js/jquery.js', __FILE__, false, '1.7.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  
}  

Put this code in functions.php

这篇关于使用“后备脚本”从Wordpress中的Google AJAX Libraries API加载jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆