I have a javascript file and I want to include jquery in this js file. Do you have a suggestion?
-
3how about this stackoverflow.com/questions/5577771/…ayush– ayush2011-09-21 08:34:10 +00:00Commented Sep 21, 2011 at 8:34
-
2Because, I want to use jquery library in my javascript file. Do you know another way?cagin– cagin2011-09-21 08:35:16 +00:00Commented Sep 21, 2011 at 8:35
5 Answers
Simply include the JavaScript for jQuery before you include your own JavaScript:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script src="path/to/your/script.js"></script>
Though others have suggested simply copying and pasting jQuery into your own JavaScript file, it's really better to include jQuery separately from a canonical source as that will allow you to take advantage of the caching that your browser and intermediate servers have done for that file.
3 Comments
[main webpage ->includes [work_app.php -> includes jQuery.js + another.js]] jQuery never gets loaded in time and yes, my script is included below jquery include.var jq = document.createElement("script");
jq.addEventListener("load", proceed); // pass my hoisted function
jq.src = "//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js";
document.querySelector("head").appendChild(jq);
function proceed () {
// jQuery load complete, do your magic
}
Comments
If you want to include a js file into a js file... javascript does not have something like an include function but you can load the file with an ajax request or add a script tag to the html with js. Check out the following link, a really interesting read!
How do I include a JavaScript file in another JavaScript file?