admin管理员组

文章数量:1418411

I need to include jQuery library in javascript file (john.js) remotely. I have tried this without any luck;

(function(d, t) {
    var g = d.createElement(t), // create a script tag
        s = d.getElementsByTagName(t)[0]; // find the first script tag in the document
    g.src = '.js'; // set the source of the script to your script
    s.parentNode.insertBefore(g, s); // append the script to the DOM
}(document, 'script'));

$( document ).ready(function() {

// My jquery works here

});

I want to fetch a script in javascript way. What is the correct way to do that ?

I need to include jQuery library in javascript file (john.js) remotely. I have tried this without any luck;

(function(d, t) {
    var g = d.createElement(t), // create a script tag
        s = d.getElementsByTagName(t)[0]; // find the first script tag in the document
    g.src = 'http://code.jquery./jquery-latest.js'; // set the source of the script to your script
    s.parentNode.insertBefore(g, s); // append the script to the DOM
}(document, 'script'));

$( document ).ready(function() {

// My jquery works here

});

I want to fetch a script in javascript way. What is the correct way to do that ?

Share Improve this question edited Dec 6, 2014 at 3:30 user198989 asked Dec 6, 2014 at 3:13 user198989user198989 4,66520 gold badges68 silver badges95 bronze badges 5
  • Have you looked at jQuery's getScript() function? api.jquery./jquery.getscript – Nate Commented Dec 6, 2014 at 3:18
  • I want to fetch Jquery, not js. And how you can use, when you havent include the jquery library ? getScript() is a jQuery function, not javascript. – user198989 Commented Dec 6, 2014 at 3:21
  • But have you looked at getScript() in the sense of examining its code to see how it works? – nnnnnn Commented Dec 6, 2014 at 3:24
  • I know that script mate. But As I said, Its JQUERY function. I need to make it in javascript way. – user198989 Commented Dec 6, 2014 at 3:27
  • 1 jQuery is written 100% in JavaScript, so obviously anything jQuery can do is doable in plain JavaScript. So, again, if you examine the jQuery source code for getScript() you can see how it works and include some version of that yourself. – nnnnnn Commented Dec 6, 2014 at 3:36
Add a ment  | 

4 Answers 4

Reset to default 3

The error here is that JQuery is not loaded yet when this code is executed:

$(document).ready(function() { .. }

As is, you get an error like this: $ is undefined (or similar)

You should use the onload event of created script element to be sure it is loaded Jquery.

This sample shown how you can achieve your goal. Good Luck.

var newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.src = 'http://code.jquery./jquery-latest.js'; // set the source of the script to your script
newScript.onload = function() {
  alert("Script is ready!");
  $(document).ready(function() {
    alert("JQuery is ready!");
  });
};
var head = document.getElementsByTagName("head")[0];
head.appendChild(newScript);

Here is the answer on how to fetch, ing from an other post :

Adding <script> element to the DOM and have the javascript run?

That beeing said another issue you may have is that when you call your

$( document ).ready(function() {

// My jquery works here

});

Jquery may not be loaded yet

so you can try a recursive function to check if jquery has been loaded with something like that (not teste)...

function launch(callBack){
    if (window.jQuery) {  
       callBack();
    } else {
        setTimeout(function(){launch(callBack);},100);
    }
}
launch(function(){
   $( document ).ready(function() {
     // My jquery works here
   });
});

You can use a bination of an XMLHttpRequest and eval to fetch the javascript file dynamically like getScript does for jQuery.

See my fiddle.

Typically the jquery is called in the html file before custom javascript files are called:

<script src="http://code.jquery./jquery-latest.js"></script>
<script src="app.js"></script>

本文标签: How to include jQuery library in Javascript without ltscript srcquotquotgtStack Overflow