0

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.

1
  • 1
    In HTML? Or through some framework? Please elaborate. Commented Nov 8, 2011 at 11:11

2 Answers 2

2

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

});

Sign up to request clarification or add additional context in comments.

Comments

0

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) { ... });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.