OK, I've got a nasty situation where everything works fine in my custom written script loader except the fact the script being loaded is executed twice.
here is complete code:
window._appCommons=window._appCommons||{};
$.loadScr = function(path, name, exportParam){
var df = $.Deferred();
if (! $( ['script[data-mname="', name, '"]'].join('') ).length ) {
window._appCommons[name] = {};
this.get(path).done(function(data){
for (var prop in exportParam) {
window._appCommons[name][prop] = exportParam[prop];
}
var scr = ['<script data-mname="' , name , '">' , data, '<\/script>'].join('');
$(document.getElementsByTagName('head')[0]).append( scr );
setTimeout(function(){ df.resolve(); },16);
});
}
return df;
}
$.loadScr('one.js', 'onejs', {'someKey':'someValue'}).done(function(){});
The loaded script - one.js only contain console.log('something...') statement and I get this twice.
I can confirm:
- that script appending -
.append( scr );happened only once - that script is "physically" loaded only once (confirmed from network panel)
and still, the same one.js, loaded once, appended once is executed twice.
So, does anyone have an idea what's happening?
UPDATE
I can now confirm that script is executed immediately after ajax loading, no matter of being appended or not. That means pure loading of javascript file, simple like $.get('one.js') executes containing javascript code. I do not know if this should happen and is there workaround or not...