Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't remove dynamically added scripts

I am appending a child element with a reference to at javascript and a stylesheet, but I would like to delete it again when there is no use of it anymore.

        var head = document.getElementsByTagName('head')[0];

        // We create the style
        var style = document.createElement('link');
        style.setAttribute("rel", "stylesheet");
        style.setAttribute("type", "text/css");
        style.setAttribute("href", '_css/style.'+app+'.css');

        var script = document.createElement('script');
        script.setAttribute("type", "text/javascript");
        script.setAttribute("src", '_scripts/_js/script.'+app+'.js');

        // And the append the style
        head.appendChild(style);
        head.appendChild(script);

This is how I append the scripts and it works perfectly.. But I can't figure out how to delete the tags again from the head tag in HTML.

Does anybody know how to remove the tags from the header tag again.. I have been searching all around Stackoverflow but nobody actually seem to have this kind of problem, but if anybody knows there is another question answering this, please tell me..

like image 346
Corfitz. Avatar asked Apr 26 '26 10:04

Corfitz.


1 Answers

In short, You Cant, Even if you remove the scripts tags, those scripts are in the memory and there is no remove them.

One hack is to write a script that identifies what the other scripts are doing (adding events, creating variables, functions etc)and then neutralize them

But the real way to solve this problem is to write your scripts in a closure,so that they are removed from the memory as soon as the program control is out of their scope.

You might also want to dig into Require.js

like image 56
Atif Avatar answered Apr 30 '26 12:04

Atif