2012-06-26 17 views
8

Tengo problemas para cargar jQuery con Javascript. Necesito cargarlo con Javascript porque hay condiciones que solo puedo conocer del lado del cliente. Se supone que el código comentado inicializa los guiones, pero no estoy teniendo suerte con ellos.Cargue jQuery dinámicamente

var script_tag = document.createElement('script'); 
script_tag.setAttribute("type","text/javascript"); 
script_tag.setAttribute("src","http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js") 
//script_tag.onload = main; // Run main() once jQuery has loaded 
//script_tag.onreadystatechange = function() { // Same thing but for IE 
    //if (this.readyState == 'complete' || this.readyState == 'loaded') main(); 
//} 
document.getElementsByTagName("head")[0].appendChild(script_tag); 

http://mybsabusiness.com/samplesites/silver/sbsa01/

Este es el sitio que los problemas están encendidas.

+1

Cómo se obtiene ningún error? ¿Qué hace 'main'? – Esailija

+2

¿Cuál es exactamente el problema? Ese código funciona perfectamente bien. http://jsfiddle.net/4Euhk/ – sachleen

+0

Muéstranos la función principal también, este código funciona para mí –

Respuesta

16

Desde el jQuerify bookmarlet:

function getScript(url, success) { 
    var script = document.createElement('script'); 
    script.src = url; 
    var head = document.getElementsByTagName('head')[0], 
     done = false; 
    // Attach handlers for all browsers 
    script.onload = script.onreadystatechange = function() { 
     if (!done && (!this.readyState 
      || this.readyState == 'loaded' 
      || this.readyState == 'complete')) { 
     done = true; 
     success(); 
     script.onload = script.onreadystatechange = null; 
     head.removeChild(script); 
     } 
    }; 
    head.appendChild(script); 
} 
getScript('http://code.jquery.com/jquery-latest.min.js',function() { 
    // Yay jQuery is ready \o/ 
});​ 
+0

¡Simplemente perfecto! Gracias amigo –

Cuestiones relacionadas