Well,
I got the same need, because of ajax calls triggered by event.
Some calls being the earliest may return later, then overwriting document
populated by the last call.
removeChild(scriptEl)
doesn't work. Changing src
attribute doesn't work either.
If jsonp
is your only way out,
and if you have control of the server response,
you can let those appended to discriminate return on the callback.
Just pass a timestamp
parameter, then check it on the callback
//callback
function dummy(res, ts) {
if(YAHOO.util.Selector.query('script[id='scriptIdPrefix+ts+']').length>0) {
do your job
} else {
do nothing
}
}
//add script
var scriptIdPrefix='myScriptId';
function ajaxCall() {
var timestamp = new Date().getTime();
var scriptId = scriptIdPrefix+timestamp;
//remove the previous script el
s=YAHOO.util.Selector.query('script[id^='+scriptIdPrefix+']');
if(s.length>0) {
document.body.removeChild(s[0]);
}
var script = document.createElement('SCRIPT');
script.id = scriptId;
script.type = 'text/javascript';
script.src = 'http://server/notify.js&callback=dummy&ts='+timestamp;
document.body.appendChild(script);
}
Assemble the result of the server notify.js
accordingly:
dummy([{"datax":"x","datay":"y"}], 1369838562792)
In this way you will have just one script element like this with
parametric id
<script id="myScriptId1369838562792" type="text/javascript"
src="http://server/notify.js?callback=dummy&ts=1369838562792"></script>
script.src='';
, or removing the script node:script.removeNode(true);
. – Amersfoortscript
. Usually you use it like this:document.getElementById('script').src='';
– Amersfoort