how to include jquery in another javascript file [duplicate]

I have a javascript file and I want to include jquery in this js file. Do you have a suggestion?

Simply include the JavaScript for jQuery before you include your own JavaScript:

<script src=""></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.

var jq = document.createElement("script");

jq.addEventListener("load", proceed); // pass my hoisted function
jq.src = "";

function proceed () {
    // jQuery load complete, do your magic


Just merge the files.

cat jquery.js >> a_javascript_file.js

Or, since you probably want jQuery first:

cat a_javascript_file.js jquery.js > tmp.js;
mv tmp.js a_javascript_file.js

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?

I suggest you to use a line of code inside your java script library.


with the help of this, you do not need to add references of two files on your page.

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .
Read More:   Remove all elements from array that match specific string

Similar Posts