Microsoft Ajax Chrome and dynamic javascript loading

In a site I need to dynamically load a javascript from javascript code, and I used this function.

1
2
3
4
5
6
function AddScriptTag(src) {
var node = document.getElementsByTagName("head")[0] || document.body;
if (node) {var script = document.createElement("script");script.type = "text/javascript";script.src = srcnode.appendChild(script);
} else {document.write("<script src='" + src + "' type='text/javascript'></script>");
}
}

Now my problem is the following, one of the dynamically loaded script is made with microsoft ajax, and it uses namespace and class like piece of code. In a page I call one of this class in this way.

1
2
3
<script type="text/javascript">

        var multiSelector = new NSxxxx.MultiSelector('nselected', <%= MultiSelectionStatus %>);

Ok, this is quite old code that is still in production, I really do not like using javascript this way, but at the time this code was written, this was the solution of choiche. The problem is that everything works fine, except in Chrome, where I have an error of type NSRilevazioni is undefined.

This is due to the fact that when this piece of script gets loaded, the NSRilevazioni script is loaded with the above function, but it still not executed, so the namespace is not registered. Too bad.

A quick and really dirty solution is the following one.

1
2
3
4
5
6
7
<script type="text/javascript">

    function InitializeNSxxxx() {
        if (typeof(NSxxxx) == 'undefined') {
            setTimeout(InitializeNSxxxx, 100);
            return;
        }

This is REALLY DIRTY :), I simply check if the NSRilevazioni namespace is still undefined, and if it is still unregistered I simply fire again the same function to be executed after 100 milliseconds.

Dirty but it works until I have time to pay this technical debt and translate everything to jQuery.

alk.