2014-11-17 12 views

risposta

11

Questa domanda è stata posta prima nella Include a Javascript File in another Javascript File. Questo pezzo di codice può andare bene quello che state cercando

function include(filename) 
{ 
    var head = document.getElementsByTagName('head')[0]; 

    var script = document.createElement('script'); 
    script.src = filename; 
    script.type = 'text/javascript'; 

    head.appendChild(script) 
} 

se questo script non funziona, vi suggerisco di guardare oltre e leggere il post di cui sopra: Include a Javascript File in another Javascript File.

Spero che questo aiuti, potrebbe non essere la risposta che stavi cercando, ma potrebbe solo aiutare.

5

È sufficiente caricare in modo asincrono come

if(yourCondition==true){ 
var d = document, 
      h = d.getElementsByTagName('head')[0], 
      s = d.createElement('script'); 
    s.type = 'text/javascript'; 
    s.async = true; 
    s.src = 'http://external/js/file/url.js'; 
    h.appendChild(s); 
} 
3

Si potrebbe usare qualcosa come:

<script type="text/javascript"> 

    if (condition == true) { 

     document.getElementsByTagName("head")[0].innerHTML += ("<script src=\"http://external/js/file/url.js\" type=\"text/javascript\"></script>"); 

    } 

</script> 
Problemi correlati