【问题标题】:Load jQuery from Google AJAX Libraries API in Wordpress with "fallback script"使用“后备脚本”在 Wordpress 中从 Google AJAX 库 API 加载 jQuery
【发布时间】:2013-08-08 09:09:28
【问题描述】:

我添加了一个 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>

喜欢这个

【问题讨论】:

    标签: php jquery wordpress google-api


    【解决方案1】:

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

    /************* 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执行的,这是服务器端而不是客户端,想象一下如果你的主机可以访问googleapis但用户浏览器无法访问它,那么你的jquery不会被加载到你的html中
    • 你不想使用 fopen(),因为它会使你的脚本变慢,而且它并不总是可用的。 html5boilerplate 的后备,在 JS 中完成,更好。
    【解决方案2】:

    @Mangesh Parte 的Answer 可以是short之类的,

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

    【讨论】:

      【解决方案3】:

      你可以试试这个:

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

      【讨论】:

        【解决方案4】:

        服务器端解决方案为什么会有很多答案?想想谁住在 中国 并且 Google 已被屏蔽

        这是正确答案(无服务器端):

        对于 Wordpress 4.5.0 +wp_add_inline_script()#

        function jquery_enqueue_with_fallback() {
            wp_deregister_script( 'jquery' );
            wp_register_script( 'jquery' , '//ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js', false, '3.1.0', true );
            wp_add_inline_script( 'jquery', 'window.jQuery||document.write(\'<script src="'.esc_url(get_template_directory_uri()).'/libs/js/jquery.js"><\/script>\')' );
            wp_enqueue_script( 'jquery' );
        }
        add_action( 'wp_enqueue_scripts' , 'jquery_enqueue_with_fallback' );
        
        • 没有服务器端检查器
        • 底部的脚本
        • 真正的后备(到本地脚本)

        注意:更改版本和您自己的本地 jQuery 源。

        【讨论】:

          猜你喜欢
          • 2016-08-12
          • 1970-01-01
          • 1970-01-01
          • 2016-09-22
          • 2016-05-22
          • 1970-01-01
          • 1970-01-01
          • 2019-02-03
          • 1970-01-01
          相关资源
          最近更新 更多