473,395 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 5029
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: David | last post by:
I have a one-line script to add an onunload event handler to the body of the document. The script is as follows: document.getElementsByTagName("BODY").onunload=function s() {alert("s")} Now...
4
by: RobG | last post by:
I have a function whose parameter is a reference the element that called it: function someFunction(el) { ... } The function is assigned to the onclick event of some elements in the HTML...
2
by: christine.nguyen | last post by:
Hello all, I have a page called test.html. Within test.html I have an iframe whose source is mypage.html. When mypage.html loads, it dynamically creates a javascript element which contains a...
5
by: stellstarin | last post by:
I have a html where fields are created and added dynamically on the client side. I use the AppendChild() call to create fields dynamically. On submit i try to get the value for all the...
4
by: max | last post by:
Hello, Based on various posts, I've come up with the following code to load data into a js array dynamically without a screen refresh. It works, but you have to push the button twice before it...
1
by: vj | last post by:
How i can populate all fileds dynamically in jsp page based on contents found in xml file? I have written jsp servlets and java class file. i transferred automatic data from jsp to servlet then to...
0
by: vijendra | last post by:
How i can populate all fileds dynamically in jsp page based on contents found in xml file?I have written jsp servlets and java class file. i transferred automatic data from jsp to servlet then to...
2
by: sylver | last post by:
Hi, Following the discussion that can be found here:...
3
by: Allen Chen [MSFT] | last post by:
Hi Richard, Quote from Richard================================================== However I also want to be able to remove the panes. I have tried to include this, but find that when I first...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.