How to remove dynamically added SCRIPT element?

T

Tereska

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

Danny

Tereska said:
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>
 
R

Randy Webb

Danny said the following on 12/16/2006 12:12 AM:
Tereska wrote:


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top