首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用“回退脚本”在Wordpress中从Google库API加载jQuery

用“回退脚本”在Wordpress中从Google库API加载jQuery
EN

Stack Overflow用户
提问于 2013-08-08 09:09:28
回答 4查看 7.3K关注 0票数 1

我添加了一个Wordpress函数,从谷歌托管的图书馆加载jQuery库

代码语言:javascript
复制
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');
}

它创造了这样的东西

代码语言:javascript
复制
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

按照html5boilerplate的建议,我如何将其包含在回退选项中

代码语言:javascript
复制
<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>

像这样

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-08-08 10:35:13

尝尝这个。用此代码替换您的代码。这会解决所有问题。

代码语言:javascript
复制
/************* 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  
}  

将此代码放入functions.php中

票数 3
EN

Stack Overflow用户

发布于 2013-08-29 07:57:22

@Mangesh Parte的Answer可以是short

代码语言:javascript
复制
<?php
    function load_external_jQuery() {
        wp_deregister_script( 'jquery' ); // deregisters the default WordPress jQuery 
        $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 if exists then register the external file  
            wp_register_script('jquery', $url);
        }
        else{// register the local file 
            wp_register_script('jquery', get_template_directory_uri().'/js/jquery.js', __FILE__, false, '1.7.2', true);  
        }
        wp_enqueue_script('jquery'); // enqueue the jquery here
    }
    add_action('wp_enqueue_scripts', 'load_external_jQuery'); // initiate the function 
?>
票数 2
EN

Stack Overflow用户

发布于 2013-08-08 09:55:45

你可以试试这个:

代码语言:javascript
复制
<script>
try { 
        //First check if jQuery exists..
    var test = jQuery.trim("test");
} catch(e) {
        //If it doesn't, add it manually..
    var script = document.createElement("SCRIPT");
    script.setAttribute("src", "js/vendor/jquery-1.9.1.min.js");
    document.head.appendChild(script);
}
</script>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18122073

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档