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