By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,537 Members | 1,750 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,537 IT Pros & Developers. It's quick & easy.

How to remove dynamically added SCRIPT element?

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.