inutan
inutan

Reputation: 10888

How to add JavaScript code in page header?

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.

Upvotes: 0

Views: 423

Answers (2)

Ferran Basora
Ferran Basora

Reputation: 3147

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

});

Upvotes: 2

the_drow
the_drow

Reputation: 19201

You need to look at this page 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 Dojo Toolkit implements it. You should take a look at require function. Example: require(["a/b"], function(b) { ... });

Upvotes: 0

Related Questions