how can I remove my appended script because it causes some problems in my app.
This is my code to get my script
var nowDate = new Date().getTime();
var url = val.redirect_uri + "notify.js?nocache=" + nowDate + "&callback=dummy";
var script = document.createElement('script');
script.src = url;
document.body.appendChild(script);
Then I have an auto load function, that causes to create another element script.
I want to get rid of the previous element that was appended before another element is added.
Basically you can remove script tag by using a function similar to this one:
Note, it use filename parameter to identify target script to remove. Also please note that target script could be already executed at the time you're trying to remove it.
What you can do is remove the script immediately after it has been loaded:
Anyway I think it is a better idea to append the script to the header (where all the other scripts reside) than to the body.
I would simply check to see if you've already added the script. Adding it and then removing is adds unnecessary complexity. Something like this should work:
In case of JSONP and your use of jQuery, why not take full advantage of jQuery's JSONP loading facilities, which do the post-clean-up for you? Loading JSONP resource like this, doesn't leave a trace in the DOM:
The
<script>
tag used for loading is removed instantly as soon it's loaded. If the loading is a success. On the other hand, failed requests don't get cleared automatically and you have to do that yourself with something like this:called at the right time. It removes all pending JSONP
<script>
tags.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. Changingsrc
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 callbackAssemble the result of the server
notify.js
accordingly:In this way you will have just one script element like this with parametric id