remove appended script javascript

后端 未结 8 1694
执笔经年
执笔经年 2020-12-29 15:35

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();
v         


        
8条回答
  •  被撕碎了的回忆
    2020-12-29 16:05

    Basically you can remove script tag by using a function similar to this one:

    function removeJS(filename){
     var tags = document.getElementsByTagName('script');
     for (var i = tags.length; i >= 0; i--){ //search backwards within nodelist for matching elements to remove
      if (tags[i] && tags[i].getAttribute('src') != null && tags[i].getAttribute('src').indexOf(filename) != -1)
       tags[i].parentNode.removeChild(tags[i]); //remove element by calling parentNode.removeChild()
     }
    }
    

    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.

提交回复
热议问题