0

I want to load jQuery from Google's CDN, and execute jQuery code if the request succeeded, but I can't seem to get it working right. Here's my code:

function loadjQuery(success, error) {
    var xhr = XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHttp');
    xhr.onload = function() { success(xhr.status, xhr.readyState); }
    xhr.onerror = function() { error(); }
    xhr.open("GET", "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js", true);
    xhr.send();
}
loadjQuery(
    function(status, state) {
        console.log(status) // 200
        console.log(state) // 4
        console.log($('body')) // Uncaught ReferenceError: $ is not defined
    },
    function() { setTimeout(loadjQuery, 10000); }
);

Any help would be really appreciated

5
  • 3
    You loaded text content from a javascript file, but did nothing with it. You likely shouldn't be using xhr for this. Commented Jan 15, 2015 at 19:25
  • I'm pretty sure loading a script with AJAX won't execute it. Commented Jan 15, 2015 at 19:26
  • How am I supposed to execute the file I'm requesting? Commented Jan 15, 2015 at 19:27
  • 3
    Create a script element, give it an onload handler, set it's src, then attach it to the dom. Commented Jan 15, 2015 at 19:27
  • 1
    Why using xhr request, why not including a script tag instead? Here, e.g, using js: stackoverflow.com/a/7474386/1414562 Commented Jan 15, 2015 at 19:29

2 Answers 2

2

Use the following code to load JQuery

(function () {

function loadScript(url, callback) {

    var script = document.createElement("script")
    script.type = "text/javascript";

    if (script.readyState) { //IE
        script.onreadystatechange = function () {
            if (script.readyState == "loaded" || script.readyState == "complete") {
                script.onreadystatechange = null;
                callback();
            }
        };
    } else { //Others
        script.onload = function () {
            callback();
        };
    }

    script.src = url;
    document.getElementsByTagName("head")[0].appendChild(script);
}

loadScript("https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js", function () {

     //jQuery loaded
     console.log('jquery loaded');
  console.log($('body') //works


});


})();
Sign up to request clarification or add additional context in comments.

Comments

0

Consider using something like RequireJs ( http://requirejs.org/). You need a modular system to fetch your google source and load it so it is available. You'll then be able to execute actions after the module is loaded. It would potentially be a lot of work to build this yourself as you've started doing.

More on AMD and requirejs can be found in the answer to this question: When does a dynamically loaded JavaScript library become available?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.