Javascript: <script src = jquery ... only if the condition is true

I am developing javascript and want to insert a script only if the condition is checked.

For example:

var a = exampleVariable; if (a == conditionIwant) { // append to head: <script src="http://code.jquery.com/jquery-1.5.js"> </ script> }; //or something like this 

How can I embed jquery.js only if the condition is true?

+7
source share
1 answer

It is really simple:

 if(somethingIsTrue) { var sc = document.createElement('script'); sc.src = 'http://code.jquery.com/jquery-1.5.js'; sc.type = 'text/javascript'; if(typeof sc['async'] !== 'undefined') { sc.async = true; } document.getElementsByTagName('head')[0].appendChild(sc); } 
+13
source

All Articles