¿Cómo agregar código JavaScript en el encabezado de la página?

Can anyone please guide how can I add a javascript block to page header at run-time?
I want to link an external js file to header at run time.

preguntado el 08 de noviembre de 11 a las 11:11

In HTML? Or through some framework? Please elaborate. -

2 Respuestas

If you want to load a script at run time with jquery you could use the ajax function:

$.ajax({
    url: 'http://example.org/myscript.js',
    dataType: 'script',
    async: false

});

respondido 08 nov., 11:15

Necesitas mirar consulta en esta página that explains On Demand Javascript.
Specifically on this function:

function ensureUploadScriptIsLoaded() {
   if (self.uploadScript) { // Already exists
     return;
   }
   var head = document.getElementsByTagName("head")[0];
   script = document.createElement('script');
   script.id = 'uploadScript';
   script.type = 'text/javascript';
   script.src = "upload.js";
   head.appendChild(script);
 }

CommonJS also specifies a way to load scripts and the Kit de herramientas de Dojo implements it. You should take a look at require function. Example: require(["a/b"], function(b) { ... });

respondido 08 nov., 11:15

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.