javascript

Add script to your page at run time

A short technical note outlining the basic approach and applicable steps for adding scripts to your page at runtime.

You can use the following function to add scripts to your page while working on the client.

var includeScriptFile = function (url, onSuccess) {
    var head = document.getElementsByTagName('head')[0];
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = url;
    script.charset = 'utf-8';
    if (onSuccess != null) {

        if (!isNaN(script.onload))
            script.onload = onSuccess;
        else {
            script.onreadystatechange = function () {
                if (this.readyState == 'complete' || this.readyState == 'loaded') onSuccess();
            }
        }
    }
    head.appendChild(script);
}

Usage:

includeScriptFile("mydomain.com/script.js",function(){alert("script Yüklendi.");})