1

What i am trying is - http://jsfiddle.net/jhrz9/1/

to remove the script generated when someone clicks the button #goBack button Now i am able to create and append script and style tags when the user clicks the #runMyCode But as soon as i go back to the previous screen using #goBack button the script stays on the screen, now what i want to do is to remove that script and create another one again when i click the #runMyCode button

Now i am trying this -

var newScript = document.createElement('script'); ;
var newTextNode=document.createTextNode($("#jsTextArea").val());
newScript.type = 'text/javascript';
newScript.appendChild(newTextNode);
document.body.appendChild(newScript);
$('#goBack').click(function(){
newTextNode.parentNode.removeChild(newTextNode);
});

But for some reason it is not working....

6
  • 2
    Note that removing a script tag does not remove the script once it's loaded into memory, so this is futile! Commented Sep 15, 2013 at 18:23
  • 2
    Why the odd mix of vanilla Javascript and jQuery? Commented Sep 15, 2013 at 18:24
  • I was working on a worklight android app thats why had to use them for simplicity Commented Sep 15, 2013 at 18:50
  • @adeneo i tried now , when i update my script in its container and then try to update the content , i see that the content in that script tag is replaced by new content, now i dont know if that script is still in memory or not? is it or it aint? Commented Sep 15, 2013 at 18:52
  • 2
    Yo, dawg, Unless the code is executed in a separate context, such as an iframe, it will still be in memory, and possibly even be able to tamper with your code. Since you already have this in a JSFiddle, try to fiddle around with it and see how it works. Commented Sep 15, 2013 at 18:58

1 Answer 1

1

you have to give the script an id.

$("#runMyCode").click(function(){
   $("#resultContainer").html($("#htmlTextArea").val());
   var newScript = document.createElement('script');
   newScript.id = "goback_script";
   var newTextNode=document.createTextNode($("#jsTextArea").val());
   newScript.type = 'text/javascript';
   newScript.appendChild(newTextNode);
   document.body.appendChild(newScript);
   var newStyle = document.createElement('style');
   var newTextNode2=document.createTextNode($("#cssTextArea").val());
   newStyle.type = 'text/css';
   newStyle.appendChild(newTextNode2);
   document.body.appendChild(newStyle);
});
$('#goBack').click(function(){
   var script = document.getElementById("goback_script");
   script.parentElement.removeChild(script);
});

jsFiddle

Sign up to request clarification or add additional context in comments.

3 Comments

This doesn't work ! The question is about removing the script from the page and not just the script tag.
You should also manually rewrite every value and function stored in that script to null if the intent is to make the contents unreadable.
Removing the script tag does not solve the original problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.