Well,
I have the same need due to ajax calls raised by an event. Some calls, which may be early, may return later, and then overwrite the document filled with the last call.
removeChild(scriptEl) does not work. Changing the src attribute also does not work.
If jsonp is your only option, and if you have control over the serverβs response, you can allow those who are added to distinguish between the return on the callback.
Just pass the 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); }
Collect the result of the notify.js server accordingly:
dummy([{"datax":"x","datay":"y"}], 1369838562792)
This way, you will only have one script element like this with a parametric id
<script id="myScriptId1369838562792" type="text/javascript" src="http://server/notify.js?callback=dummy&ts=1369838562792"></script>
source share