Toolkit:script

From Surebert wiki

Contents

Overview

Available since tags 4.82.

You can use sb.script in order to dyanmically load additional javascript into the head of your document and have it execute. While you can esily use sb.ajaz for loading additional javascript on the same site, sb.ajax does not allow you to load data from another domain. To get around thisyou can use sb.script, however, you should be aware that this could be dangerous if you do not trust the 3rd party you are loading the javascript from.

sb.script returns an sb.element that represents the script tag which was dynamically created.

Properties

src

The src of the javascript data

charset

The charset defaults to UTF-8

type

Defaults to text/javascript

Methods

By default sb.script instances inherit all the methods of Element.prototype. It also standardizes onload for IE

onload()

Fires after the script has loaded and can be used to interact with the data in the script. It can also be used to remove the script data from the page after it has executed to prevent memory leaking in long lived scripts.

load()

Fetch the data from the src specified when creating the instance.

remove()

Removes the script from the DOM and cleans up the attributes in IE

Example

javascript code

var script = new sb.script({
    src : 'http://someotherdomain.com/test/script',
    onload : function(){
	//alert teh head before removing
        alert($('head').innerHTML);
        this.remove();
	//alert it after
        alert($('head').innerHTML);
 
     }
});
 
script.load();