SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict TegSkywalker's Avatar
    Join Date
    Nov 2001
    Location
    USA
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with loading backups of JS files from CDN

    I'm using the asynchronous example from http://css-tricks.com/snippets/jquer...f-not-present/ to load a backup jQuery CDN if the main Google fails or goes undefined for some reason. The default script loads the backup (ASPNET CDN) but if I want to add a local copy backup to the ASPNET CDN, it will not load if the ASPNET CDN is down. I tried different variations with no success. Here is the code:


    Code JavaScript:
    // Setup backups
    var jqbackup1 = '//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.4.min.js';
    var jqbackup2 = '/js/jquery-1.4.4.min.js';
     
    // Setup getScript function
    function getScript(url, success) {
     
        var script = document.createElement('script');
        script.src = url;
     
        var head = document.getElementsByTagName('head')[0],
        done = false;
     
        // Attach handlers for all browsers
        script.onload = script.onreadystatechange = function() {
     
            if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
     
                done = true;
     
                // callback function provided as param
                success();
     
                script.onload = script.onreadystatechange = null;
                head.removeChild(script);
     
            }; // End !done && this.readystate
     
        }; // End script.online = onreadystate
     
        head.appendChild(script);
     
    }; // End getScript function
     
     
    // If main Google jQuery load is undefined or does not exist
    if (typeof jQuery == 'undefined') {
     
        // If another JS lib like mootools is using $
        if (typeof $ == 'function') {
     
            // warning, global var
            thisPageUsingOtherJSLibrary = true;
     
        }
     
        // Load first backup
        getScript(jqbackup1, function() {
     
            // If first backup fails
            if (typeof jQuery == 'undefined') {
     
                // First backup fails, so load local copy
                getScript(jqbackup2, function() {
                    // Should anything extra be here?
                });
     
            // First backup worked
            } else {
                // Run jQuery code
            }
     
        }); // End getscript for jqbackup1
     
     
    // Main Google jQuery loaded fine
    } else {
        // Run your jQuery Code
    }

  2. #2
    SitePoint Addict TegSkywalker's Avatar
    Join Date
    Nov 2001
    Location
    USA
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am guessing that maybe this block of code is giving me the issues because either the undefined check isn't seeing it or the getscript for jqbackup2 is trying to access something that may or may not be undefined anymore or is too deep.

    I've tested putting in wrong version numbers to force the script to call in the backups, but when I fudge the version number for jqbackup1, the local copy (jqbackup2) is not loading according to the Net area of Firebug and the scripts using jQuery.

    Code:
        // Load first backup
        getScript(jqbackup1, function() {
            
            // If first backup fails
            if (typeof jQuery == 'undefined') {
                
                // First backup fails, so load local copy
                getScript(jqbackup2, function() {
                    // Should anything extra be here?
                });
                    
            // First backup worked
            } else {
                // Run jQuery code
            }
            
        }); // End getscript for jqbackup1

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    It may not be possible for the web browser to process the updated DOM and load the script while the script is already running.

    One possible solution to that is to delay the check using setTimeout

    Another solution is to not rely on javascript for the task, and to instead request the jQuery library from a separate PHP script that attempts to retrieve the library from multiple sources.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •