470,625 Members | 1,940 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,625 developers. It's quick & easy.

How to remove dynamically added SCRIPT element?

I want to delete script added before.
I'm adding script dynamically and i'm removing later.
Why it is still working?

I have something like this:

<html>
<head>
<title>JS Script Remove</title>
</head>

<body>

<a href="javascript:void(0)" onclick="javascript:showText()">Why it's
still working???!!!</a>

<script>

//making new script object
var objScript = window.document.createElement('script');
objScript.text = "function showText(){alert('I am still working!
WHY?');}"
objScript.type = 'text/javascript';
objScript.id = 'myScriptId';

//adding my script object to head element
var objHead = window.document.getElementsByTagName('head')[0];
objHead.appendChild(objScript);

//OK script added
alert(objHead.innerHTML);

//removing script from head
objHead.removeChild(objScript);

//script has gone
alert(objHead.innerHTML);

//nothing special in body
alert(document.body.innerHTML);

</script>

</body>
</html>

Dec 16 '06 #1
2 4780
Tereska wrote:
I want to delete script added before.
I'm adding script dynamically and i'm removing later.
Why it is still working?

I have something like this:

<html>
<head>
<title>JS Script Remove</title>
</head>

<body>

<a href="javascript:void(0)" onclick="javascript:showText()">Why it's
still working???!!!</a>

<script>

//making new script object
var objScript = window.document.createElement('script');
objScript.text = "function showText(){alert('I am still working!
WHY?');}"
instead of .text, which it doesn't have AFAIK, use .innerText property for IE
and .textContent for gecko browser, as for Opera, opera does .innerText.

Danny
objScript.type = 'text/javascript';
objScript.id = 'myScriptId';

//adding my script object to head element
var objHead = window.document.getElementsByTagName('head')[0];
objHead.appendChild(objScript);

//OK script added
alert(objHead.innerHTML);

//removing script from head
objHead.removeChild(objScript);

//script has gone
alert(objHead.innerHTML);

//nothing special in body
alert(document.body.innerHTML);

</script>

</body>
</html>
Dec 16 '06 #2
Danny said the following on 12/16/2006 12:12 AM:
Tereska wrote:
<snip>
> var objScript = window.document.createElement('script');
objScript.text = "function showText(){alert('I am still working!
WHY?');}"
instead of .text, which it doesn't have AFAIK, use .innerText property for IE
and .textContent for gecko browser, as for Opera, opera does .innerText.
To date, the only browsers that have been found that doesn't support the
..text property is Safari and iCab. Other than that, .text is very widely
supported.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 16 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

20 posts views Thread by David | last post: by
4 posts views Thread by max | last post: by
3 posts views Thread by Allen Chen [MSFT] | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.