How can I remove a some javascript from my webpage?

andrew

With jquery.load() I'm loading some html into my page from a file which also contains javascript functions.

later I try to remove the html and the javascript using jquery.empty but it seems once the script is parsed by the browser I can't get rid of it, so I'm looking for suggestions on how to do this.

Below is the test source:

index.html

<head>
<script src="//code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
<script>
$(function(){
  $('#loadEmbScrpt').click(function(){
       $('#embDiv').load('http://maccyd10.hostoi.com/test.html');      
     });
   $('#remEmbScrpt').click(function(){
      $('#embDiv').empty();
   });
 });

<body>
<div id="embDiv"></div>
<div id="output"></div>
<input type="button" id="loadEmbScrpt" value="embed script into page"/>
<input type="button" id="remEmbScrpt" value="remove embedded script from page"/>
<input type="button" id="testButton" value="run embedded script test function"/>
</body>

test.html

<script>
$('#testButton').on('click',function(){
 $('#output').append("<p>test</p>");
});
</script>

And here is a link to the above in action (I could not post this to jsfiddle due to XSS protection).

http://maccyd10.hostoi.com

davnicwil

Have a play with this http://jsfiddle.net/6eWRQ/

As others have alluded to, the DOM and the Javascript VM are two separate systems within the browser.

The DOM is the browser's internal model of the HTML document to be rendered. It deals with HTML elements such as <script> and their position within the HTML document.

The Javascript VM deals with javascript code - it deals with running any Javascript code within <script> tags or pulled in from external js files.

it seems once the script is parsed by the browser I can't get rid of it

You can of course remove the <script> element from the DOM - but after the browser has parsed it this will have zero effect on the javascript VM - because then the javascript code inside the tag it has already been consumed and executed by the VM. Once the code has run, it cannot be un-run.

In the case of the example jsfiddle this is even clearer - removing a <script> element containing a function doesn't mean you can't call that function any more, once the code inside the <script> has already been executed by the VM. The VM has its own internal model of the function and changes to the original code in the DOM after it has been executed are simply irrelevant to it.

Removing the containing <script> element from the DOM using javascript is effectively pointless in this situation - it's just redundant. The DOM rendering engine has no use for it because it's not a visual element, and the javascript VM has no use for it because it's already-processed input.

To do what you want you should remove the #remEmbScrpt button, and instead use the following to undo the binding performed by the loaded script:

$('#testButton').off('click');

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I preview a PDF file generated from an external API in an iframe of my webpage, using Javascript?

From Dev

how can i put javascript result into a webpage as the values states undefined for some reason

From Dev

how can i put javascript result into a webpage as the values states undefined for some reason

From Dev

How can i remove some file from fineuploader before upload

From Dev

How can I remove some elements from a list?

From Dev

How to remove html tag from webpage with some other text?

From Dev

How can I remove data type information from my xml in MarkLogic Javascript?

From Dev

When embedding PageDown editor in my webpage, how I can I stop it from doing conversion in real time?

From Dev

When embedding PageDown editor in my webpage, how I can I stop it from doing conversion in real time?

From Dev

How can I stop elpy from overriding some of my keybindings?

From Dev

How can I stop elpy from overriding some of my keybindings?

From Dev

How can I find some distance from my location with Swift

From Dev

How can I print keys from json on my webpage one by one?

From Dev

How can I print keys from json on my webpage one by one?

From Dev

How can I update the time from chosen timezone every minute on my webpage?

From Dev

How can I remove Watchkit from my App?

From Java

How can I remove an array from my redux store?

From Dev

How can i remove this sidebar from my html page?

From Dev

How can I remove crashlytics from my iOS app?

From Dev

How can I remove the smell of cigarettes from my computer?

From Dev

How can I remove the values apostrophes of my choice from a line?

From Dev

How can I remove the .php extension from my PHP page?

From Dev

How can I remove DRM from my Kindle books?

From Dev

How can I download an HTML webpage including JavaScript-generated content from the terminal?

From Dev

How can I embed the new GIFV format on my webpage?

From Dev

How can I hide a div when my webpage is viewed in Lynx?

From Dev

How can I improve my webpage code to be scalable for smaller sizes?

From Dev

How can I hide a div when my webpage is viewed in Lynx?

From Dev

How can I run the interval on my webpage only once? (jquery)

Related Related

  1. 1

    How can I preview a PDF file generated from an external API in an iframe of my webpage, using Javascript?

  2. 2

    how can i put javascript result into a webpage as the values states undefined for some reason

  3. 3

    how can i put javascript result into a webpage as the values states undefined for some reason

  4. 4

    How can i remove some file from fineuploader before upload

  5. 5

    How can I remove some elements from a list?

  6. 6

    How to remove html tag from webpage with some other text?

  7. 7

    How can I remove data type information from my xml in MarkLogic Javascript?

  8. 8

    When embedding PageDown editor in my webpage, how I can I stop it from doing conversion in real time?

  9. 9

    When embedding PageDown editor in my webpage, how I can I stop it from doing conversion in real time?

  10. 10

    How can I stop elpy from overriding some of my keybindings?

  11. 11

    How can I stop elpy from overriding some of my keybindings?

  12. 12

    How can I find some distance from my location with Swift

  13. 13

    How can I print keys from json on my webpage one by one?

  14. 14

    How can I print keys from json on my webpage one by one?

  15. 15

    How can I update the time from chosen timezone every minute on my webpage?

  16. 16

    How can I remove Watchkit from my App?

  17. 17

    How can I remove an array from my redux store?

  18. 18

    How can i remove this sidebar from my html page?

  19. 19

    How can I remove crashlytics from my iOS app?

  20. 20

    How can I remove the smell of cigarettes from my computer?

  21. 21

    How can I remove the values apostrophes of my choice from a line?

  22. 22

    How can I remove the .php extension from my PHP page?

  23. 23

    How can I remove DRM from my Kindle books?

  24. 24

    How can I download an HTML webpage including JavaScript-generated content from the terminal?

  25. 25

    How can I embed the new GIFV format on my webpage?

  26. 26

    How can I hide a div when my webpage is viewed in Lynx?

  27. 27

    How can I improve my webpage code to be scalable for smaller sizes?

  28. 28

    How can I hide a div when my webpage is viewed in Lynx?

  29. 29

    How can I run the interval on my webpage only once? (jquery)

HotTag

Archive