Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if jquery has been loaded, then load it if false

Does anyone know how to check if jquery has been loaded (with javascript) then load it if it hasnt been loaded.

something like

if(!jQuery) {     //load jquery file } 
like image 265
seventeen Avatar asked Dec 01 '09 19:12

seventeen


People also ask

How do you check is jQuery loaded or not?

Basically, the most reliable way to check if jQuery has been loaded is to check typeof jQuery — it will return "function" if jQuery was already loaded on the page, or "undefined" if jQuery hasn't been loaded yet.

How do you check if page is loaded completely?

You can check the document. readyState property. From MDN: Returns "loading" while the document is loading, "interactive" once it is finished parsing but still loading sub-resources, and "complete" once it has loaded.


1 Answers

Maybe something like this:

<script> if(!window.jQuery) {    var script = document.createElement('script');    script.type = "text/javascript";    script.src = "path/to/jQuery";    document.getElementsByTagName('head')[0].appendChild(script); } </script> 
like image 200
Daniel LeCheminant Avatar answered Sep 23 '22 20:09

Daniel LeCheminant