0

I am building an AJAX website which uses AJAX to replace links. In the different pages they have included different <script>s:

 <script class="ajax">
     var blah = blah;
     function abc(){
         Blah.blah.blah();
     }
 </script>
 <p>
     Blah blah blah blah blah...
 </p>

And when the client clicks the link, the page will load the new contents and remove the scripts.

$("script.ajax").remove();
$("p").html(data);

But here's the problem. Although the script tag is removed, the function abc is still there, and I can't get rid of it. (variables also) I can't use :

abc = undefined;

since every page has different functions. Is it possible to achieve that or is it not even possible? Thanks.

Derek 朕會功夫
  • 88,688
  • 41
  • 174
  • 241
  • Maybe this can help: http://stackoverflow.com/questions/1596782/how-to-unset-a-javascript-variable – Manatok Mar 09 '12 at 05:45
  • I want to remove everything, not just variables and function. – Derek 朕會功夫 Mar 09 '12 at 05:58
  • If the script runs, then it modifies the global context (the `window` object), and there's no way to undo that except for nulling out all the added properties (ie, global variables and functions). – Pointy Mar 09 '12 at 06:11
  • You might try loading the markup into a document fragment or say a DIV element that isn't in the DOM. Then remove the script elements before putting the fragment or DIV.innerHTML into the document. As pointy says, once the script has run, it's in the global space and that's that. – RobG Mar 09 '12 at 07:27

0 Answers0