rewrite jquery insertion functions script (3 posts)

  1. micasuh
    Posted 5 years ago #

    Paul Irish wrote a new version of jquery insertion into the [HTML5 Boilerplate project that looks like this:

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
        <script>!window.jQuery && document.write(unescape('%3Cscript src="js/libs/jquery-1.4.2.js"%3E%3C/script%3E'))</script>

    Here's Paul's explanation: http://paulirish.com/2010/the-protocol-relative-url/

    I'm very interested to port this over verbatim for WordPress projects using the wp_enqueue_script.

    function add_jquery_script() {
          wp_deregister_script( 'jquery' ); // get rid of WP's jQuery
          wp_register_script( 'jquery_cdn', '//ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js', array(), '1.0', true );
          wp_enqueue_script( 'jquery_cdn' );
          echo'<script>!window.jQuery && document.write(unescape(\'%3Cscript src="' . get_bloginfo('template_directory') . '/js/libs/jquery-1.4.2.min.js"%3E%3C/script%3E\'))</script>';

    Two main problems:

    1. First register script line spits out absolute path rather than relative. The double-forward slashes should not have WordPress' URI preceding the link.
    2. The echo forces the code in at the top (because it likely doesn't belong in this function in this way).

    It'd be cool to get both of these versions working with the wp_enqueue_script but I realize it's likely not possible.

  2. micasuh
    Posted 5 years ago #

    I hope someone sees this who knows the answer b/c I'd love to implement this in my projects.

  3. jmh
    Posted 5 years ago #

    Hey, I've been wondering this same thing. I found this blog post and used his idea for problem number 1:


    I am still searching for the solution to providing a local fallback using the proper wp_enqueue_script function

Topic Closed

This topic has been closed to new replies.

About this Topic