Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load jQuery with Javascript and use jQuery

I am appending the jQuery library to the dom using:

 var script = document.createElement('script');  script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js';  script.type = 'text/javascript';  document.getElementsByTagName('head')[0].appendChild(script); 

However when I run:

jQuery(document).ready(function(){... 

The console reports the error:

Uncaught ReferenceError: jQuery is not defined 

How do I load jQuery dynamically as well as use it once it is in the dom?

like image 436
ThomasReggi Avatar asked Apr 11 '12 20:04

ThomasReggi


People also ask

Can you use both jQuery and JavaScript together?

jQuery is a JavaScript library, so it operates on top of JavaScript. It cannot exist on its own, so you can't use one over the other. You can use just JavaScript or JavaScript and jQuery. jQuery was introduced to make development with JavaScript easier.

How do I load jQuery into JavaScript?

To add jQuery and JavaScript to your web pages, first add a <script> tag that loads the jQuery library, and then add your own <script> tags with your custom code.

Can I write jQuery in JavaScript 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.

Can we use jQuery in script tag?

Step 1: Firstly, we have to open that Html file in which we want to add the jQuery using CDN. Step 2: After then, we have to place the cursor between the head tag just before the title tag. And, then we have to use the <script> tag, which specify the src attribute for adding.


1 Answers

There's a working JSFiddle with a small example here, that demonstrates exactly what you are looking for (unless I've misunderstood your request): http://jsfiddle.net/9N7Z2/188/

There are a few issues with that method of loading javascript dynamically. When it comes to the very basal frameworks, like jQuery, you actually probably want to load them statically, because otherwise, you would have to write a whole JavaScript loading framework...

You could use some of the existing JavaScript loaders, or write your own by watching for window.jQuery to get defined.

// Immediately-invoked function expression (function() {   // Load the script   const script = document.createElement("script");   script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js';   script.type = 'text/javascript';   script.addEventListener('load', () => {     console.log(`jQuery ${$.fn.jquery} has been loaded successfully!`);     // use jQuery below   });   document.head.appendChild(script); })();

Just remember that if you need to support really old browsers, like IE8, load event handlers do not execute. In that case, you would need to poll for the existance of window.jQuery using repeated window.setTimeout. There is a working JSFiddle with that method here: http://jsfiddle.net/9N7Z2/3/

There are lots of people who have already done what you need to do. Check out some of the existing JavaScript Loader frameworks, like:

  • https://developers.google.com/loader/ (no longer documented)
  • http://yepnopejs.com/ (deprecated)
  • http://requirejs.org/
like image 148
Anders Marzi Tornblad Avatar answered Sep 24 '22 15:09

Anders Marzi Tornblad