javascriptappendappendchildcreateelement

remove appended script javascript


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.


Solution

  • I did some more tests, and before you'll get a correct answer to your question (hope there is a one) you can try this:

    <button onclick="foo()">ShowHTML</button>
    <script>
    (function foo(){
        var b=function moo(){
            var c=document.getElementsByTagName('script');
            alert(document.body.innerHTML);
            c[0].parentElement.removeChild(c[0]);
            alert(document.body.innerHTML);
        }
        var a=setTimeout(b,1000);
        b=null;
    })();
    foo=null;
    </script>
    

    This is just a test code, but it contains an idea, how you possible could solve the problem. It removes <sript> from the DOM, and the last line destroys all functionality of the script.

    (The code also has a little detail, which shows, that setTimeout will do eval(), no matter how it is argumented...?)