How to add a backup javascript file in WordPress that initially loads off internet

I’m sure you’re all aware of loading files like jquery off google. I’m trying to load a file similarly, but I want to have a backup of that file on my server incase the online version is down, and I don’t know how to go about that in WordPress.

Here’s what I have currently which loads it from the url

wp_deregister_script('html5shiv');
wp_register_script('html5shiv', ("http://html5shim.googlecode.com/svn/trunk/html5.js"),false);
wp_enqueue_script('html5shiv');

Related posts

Leave a Reply

2 comments

  1. I think this is what you are looking for.

    C&P in case someone came back later looking for this and the site is unavailable:

    <?php
    $url = 'http://ajax.googleapis.com/ajax/libssss/jquery/1.6.4/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.6.4/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', bloginfo('template_url').'/js/libs/jquery-1.6.1.min.js', __FILE__, false, '1.6.4', true); // register the local file
            wp_enqueue_script('jquery'); // enqueue the local file
        }
    add_action('wp_enqueue_scripts', 'load_local_jQuery'); // initiate the function
    }
    ?>
    
  2. I’m not familiar with the WordPress queuing system, but here is some general code for javascript with fallback. You may want to post this question at http://wordpress.stackexchange.com where WP api questions tend to get better answers. Here is a fallback mechanism

    //add a property to the window object in foo.js
    window.banana = 'peeled';
    
    <head>
      <script src="https://mysite.com/foo.js" type="text/javascript"></script>
    
      <script type="text/javascript">    
        //fallback mechanism if not available    
        if (!window.Banana) { document.write(unescape("%3Cscript src='/localfoo.js' type='text/javascript'%3E%3C/script%3E")); }
    
      </script>
    </head>