Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to include jquery in another javascript file [duplicate]

Tags:

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

like image 298
cagin Avatar asked Sep 21 '11 08:09

cagin


People also ask

Can I add jQuery in a JS file?

Your JavaScript file ( scripts. js ) must be included below the jQuery library in the document or it will not work. Note: If you downloaded a local copy of jQuery, save it in your js/ folder and link to it at js/jquery. min.

How do I include a JavaScript file in another?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file.

How use jQuery inside JavaScript?

Implementing Your Own jQuery and JavaScriptThe JavaScript code can be added inside the <script> element, or the src attribute of the <script> element can point to the location of a separate JavaScript document. Either way, the JavaScript will be loaded in the same manner.

Can you have two JavaScript files?

Yes, it is possible to call two JavaScript files in one HTML file. This can be done by using the HTML tag. The tag can be used to include an external JavaScript file.


2 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.

like image 95
Michael Aaron Safyan Avatar answered Oct 06 '22 01:10

Michael Aaron Safyan


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   } 
like image 37
neaumusic Avatar answered Oct 06 '22 01:10

neaumusic