Javascript – How to add jQuery from a Javascript file

When you create a javascript file, you may want to make sure you can use jQuery. To not have it twice and maybe add incompatibility, just add at the beginning of the javascript file this:

if (typeof jQuery 'undefined')
   var script - document.createElement ('script');
   script.src - 'http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js';
   script.type - 'text/javascript';
   document.getElementsByTagName ('head')[0].appendChild (script);
}
Publicités

Leave a Reply