39605

How to dynamically remove script from a page

Question:

What i am trying is - <a href="http://jsfiddle.net/jhrz9/1/" rel="nofollow">http://jsfiddle.net/jhrz9/1/</a>

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....

Answer1:

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); });

<a href="http://jsfiddle.net/e3PvY/" rel="nofollow">jsFiddle</a>

Recommend

  • Get byte[] from
  • responsive table with variable column number
  • Using third party libraries
  • html2canvas creates a canvas with a width and height of zero?
  • Websocket error by closing a page via browser: “An established connection was aborted by the softwar
  • How to store value from one dropdown list to another dropdown list
  • How to initialize global variable at each class invocation?
  • how to replace TreeView's selected node with newly created node
  • pure javascript dom dynamic insert, update and delete
  • Javascript - Waiting for event before proceeding
  • After converting my FBX file to a .gltf, the model is incredibly small, why?
  • react native create element with string
  • MySQL multiple IN conditions to subquery with same table
  • Why does the following throw an “Object doesn't support property or method 'importNode
  • Is it possible to get the word under the mouse cursor in a ``?
  • BeautifulSoup difference between findAll and findChildren
  • NHibernate manually control fetching
  • Getting error 'Cannot read property 'document' of undefined' while importing exp
  • Application level floating views with navigation in Android
  • AndEngine Applying Transparancy to AndEngine View
  • Using a canvas object in a thread to do simple animations - Java
  • How do I get HTML corresponding to current DOM tree?
  • Ionic 2 storage is not cleaning up on uninstall - Only for signed APK
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • JQuery Internet Explorer and ajaxstop
  • JSON response opens as a file, but I can't access it with JavaScript
  • How do I alternate colors in Flat List (React Native)
  • Listbox within Listbox and scrolling trouble in Windows Phone 7 Silverlight
  • Android full screen on only one activity?
  • Avoid links criss cross / overlap in d3.js using force layout
  • FileReader+canvas image loading problem
  • Updated Ionic CLI but shows previous version (Windows)
  • Change an a tag attribute in JavaScript based on screen width
  • jquery mobile loadPage not working
  • Unanticipated behavior
  • Authorize attributes not working in MVC 4
  • Busy indicator not showing up in wpf window [duplicate]
  • Conditional In-Line CSS for IE and Others?
  • Python/Django TangoWithDjango Models and Databases
  • Net Present Value in Excel for Grouped Recurring CF