1
(function() {

    setTimeout(function(){
        $('#core-notify').remove();
        $('#yourDynamicGeneratedScript').remove();
        $('#yourDynamicGeneratedScriptHtml').remove();

    context = $("#context").val();
        lid =  $("#userid").val();
        tid = $("#tenentId").val();
        dp_url = $("#dpUrl").val();

    var script_tag = document.createElement('script');
    script_tag.setAttribute("type","text/javascript");
    script_tag.setAttribute('id', 'yourDynamicGeneratedScript');

    script_tag.setAttribute("src",
    "https://sa.nlpbots.com/lightbot/js/light_bot.js");
      (document.getElementsByTagName("head")[0]).appendChild(script_tag);

    console.log((document.getElementsByTagName("head")[0]));

    },1000);
})(); 

This code removes a script by tag id but it only removes it from the head tag, not from the DOM file.

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
  • What do you mean with `only from head tag not from DOM file`? DOM is not a file but a tree representation of the html after it was parsed. So if you can remove it from `head` then it is also removed from DOM. Beside that, what do you try to achieve with that? – t.niese Jan 10 '17 at 06:04
  • i append "light_bot.js" to head from diff rant server ,i need to call that js again and again thats why "light_bot.js " this js append as per call , so i remove that light_bot.js from head tag but its not remove from dom. due to duplicate light_bot.js present other js function not working like click() – Sharad Ambhore Jan 10 '17 at 06:16
  • Removing a `script` tragt from the DOM does not remove the scripts that where evaluated, not any changes they did (e.g. adding event listeners). So if your `light_bot.js` added event listeners, you also need to remove them manually, or the script loaded with `light_bot.js` has to provide a cleanup function that would remove all listeners it has added. – t.niese Jan 10 '17 at 06:48

0 Answers0