php - 使用 "fallback script"从 WordPress 中的 Google AJAX 库 API 加载 jQuery

标签 php jquery wordpress google-api

我添加了一个 Wordpress 函数来从 Google 托管库加载 jQuery 库

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');
}

它创建了这样的东西

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

如何按照 html5boilerplate 建议将其包含在后备选项中

<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>

像这样

最佳答案

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

/************* 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

关于php - 使用 "fallback script"从 WordPress 中的 Google AJAX 库 API 加载 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18122073/

相关文章:

jquery - 如何在提交前验证 jQuery 中的复选框是否已选中

javascript - Asp.Net MVC 中的无限滚动与 jQuery/AJAX 问题

php - 如何自动清除 wp session 条目的选项表

php - ElasticSearch 查询卡在 laravel 作业队列中

php - ORDER BY 中的 MySQL rand() 函数

javascript - 在一个事件中提交两个表单

javascript - 带搜索的分页

javascript - 进度圈,颜色之间有空格

javascript - 单击链接到页面上某个部分的菜单项时,如何隐藏汉堡包菜单?

css - 如何在博客文章之间添加图片