peroxide
peroxide

Reputation: 696

how to add a js condition

Hi
I want to write a condition in JS that will result in adding an external js.
For example if the condition is x > 2 and it is true then i want it to load a different external js file but if its false then load another js file.
How can i write it?
Thanks

Upvotes: 1

Views: 1122

Answers (2)

FatherStorm
FatherStorm

Reputation: 7183

  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  if (x > 2){
     fileref.setAttribute("src", "/js/file1.js")
 }else{
     fileref.setAttribute("src", "/js/file2.js")
 }
 document.body.appendChild(fileref);

Upvotes: 2

Daniel
Daniel

Reputation: 2381

you can write:

if(conditon)
   $('body').append("<script src='whatever'><" + "/script>");

Upvotes: 0

Related Questions