How to remove dynamically added SCRIPT element?

Discussion in 'Javascript' started by Tereska, Dec 16, 2006.

  1. Tereska

    Tereska Guest

    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>
     
    Tereska, Dec 16, 2006
    #1
    1. Advertising

  2. Tereska

    Danny Guest

    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>
     
    Danny, Dec 16, 2006
    #2
    1. Advertising

  3. Tereska

    Randy Webb Guest

    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/
     
    Randy Webb, Dec 16, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bennett Haselton
    Replies:
    1
    Views:
    516
    John M Deal
    Nov 8, 2004
  2. Jeffrey Todd
    Replies:
    1
    Views:
    4,120
    Peter Blum
    Jun 2, 2005
  3. Replies:
    4
    Views:
    2,537
  4. sylver
    Replies:
    2
    Views:
    91
    sylver
    Jun 20, 2007
  5. BananaQuaalude

    Remove child element that was dynamically created

    BananaQuaalude, Dec 17, 2007, in forum: Javascript
    Replies:
    2
    Views:
    555
    My Pet Programmer
    Dec 23, 2007
Loading...

Share This Page