Does jQuery .remove() clear out loaded javascript when it is used to remove a script tag?

前端 未结 2 589
攒了一身酷
攒了一身酷 2020-11-30 03:08

As the title says, if I remove a script tag from the DOM using:

$(\'#scriptid\').remove();

Does the javascript itself remain in memory or i

2条回答
  •  野性不改
    2020-11-30 03:49

    If your scripts have already executed removing the DOM elements are not going to get rid of them. Go to any page with JavaScript, open up your preferred javascript console and type $("script").remove(). Everything keeps running.

    And this demonstrates @Kolink answer:

    http://jsfiddle.net/X2mk8/2/

    HTML:

    Javascript:

    $("script").remove();
    // or $("#yourDynamicGeneratedScript").remove();
    
    test(2);
    test(3);
    test(4);
    
    function test(n) {
      $output = $("#output")
      $output.append("REDEFINED! " + n + "
    ") } test(5); test(6); test(7);

提交回复
热议问题