Home > Uncategorized > Adding Javascript file dynamically using Atlas

Adding Javascript file dynamically using Atlas

December 7, 2005

The question came up on the Microsoftgadgets forum:

Is there an easy way to include one .js file within another?

Yes there is away to add a javascript file to your page dynamically by using the Web.Network.createRequest method.

function OnScript(script)
{
Web.Utility.Script.attachScript(script, document.body, null);
}
var r = Web.Network.createRequest(Web.Network.Type.Script, jsToAddUrl, null, OnScript);

r.execute();

OnScript is the asynchronous callback that handles the Response of the Request. The arguments for attachScript causes the script to be attached to the document Body. The third argument is a placeholder for a callback method to respond once the script has been attached.

In order to get the script we create a request of type Script, passing the requested js file’s url and the callback method to handle the request. The third argument is an open slot for an object used to persist store between requests.

And that’s it, once attached the script runs any open JavaScript and the methods are available for use.

%d bloggers like this: