Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6005217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:22:08+00:00 2026-05-23T01:22:08+00:00

It is very curious why the script tag only runs once after inserted into

  • 0

It is very curious why the script tag only runs once after inserted into the dom.

script = document.createElement('script');
script.type = 'text/javascript'

script
//output: <script type=​"text/​javascript">​</script>​

document.body.appendChild(script);

script.innerHTML = 'console.log("Attempt one")'
//output Attempt one

script.innerHTML = 'console.log("Attempt two")'

script.innerHTML = 'console.log("Attempt three")'

//second and third attempts emit no output

Is there any way to allow the other changes to script text to run?

I’ve already tried wrap it into a function and no success…I couldn’t find any explanation why the log runs the first time and not the following ones

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:22:08+00:00Added an answer on May 23, 2026 at 1:22 am

    Just to clarify here, that the code above could be a bad approach, etc.
    But the question really refers to the behaviour and why does it happens.

    After playing around in the past minutes I could notice that the javascript will only run once for each script tag once it is both appended to the DOM and has a content.

    So for example, you could have a script tag appended to the DOM without any content…
    that wouldn’t fire the javascript. But as soon as you add content to that tag with innerText, innerHTML, etc…it will fire the javascript straight away.

    It is quite interesting to notice that each script tag, seems to handle a flag which is set to false every time javascript run for that specific tag, then after this happens it won’t fire again, no matter why.

    here are some tests that are interesting too:

    Normal behaviour

    var script = document.createElement('script');
    
    document.body.appendChild(script);
    
    script.innerHTML = 'alert("attemp one")';
    // outputs alert
    script.innerHTML = 'alert("attemp two")';
    // no output
    

    Deep Cloning node

    var clone = script.cloneNode(true);
    // <script>alert("attemp two")</script>
    
    document.body.replaceChild(clone, script);
    // no output
    

    Simple Cloning node

    var clone = script.cloneNode();
    // <script></script>
    
    clone.innerHTML = 'alert("attemp two")';
    
    document.body.replaceChild(clone, script);
    // no output
    

    Replacing for new tag

    var newScript = document.createElement('script');
    
    newScript.innerHTML = 'alert("attempt three")';
    
    document.body.replaceChild(newScript, script);
    // output alert
    

    So as you can see the script tag will hold the alreadyExecuted attribute no matter why, if you clone it by generating an exact copy or not…

    Which makes me think if is there any way to set this flag to its original initial value instead of creating a new script tag.

    But in the worst case if you really need to use this. It is not difficult to create an action that will get the script , create a new one with the same attributes if you have some and then replace it with the old but with new content. The javascript would run like the first time.

    The following script can easily be called and that will replace the old script tag for a new one and execute the new javascript by simply providing the script tag you wish to replace contents.

    function innerScript(element) {
    
        var newElement = document.createElement('script'),
            attr;
    
        for (attr = 0; attr < element.attributes.length; attr += 1) {
            newElement.setAttribute(element.attributes[attr].name, element.attributes[attr].value);
        }
    
        element.parentNode.replaceChild(newElement, element);
        return newElement;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm curious what's the view on things that compile into javascript e.g. GWT, Script#
This is mostly a theoretical question I'm just very curious about. (I'm not trying
I found this web site (photoblog) http://www.OneReaction.net/ and I am very curious how this
Very simply put: I have a class that consists mostly of static public members,
Very simply put, I have the following code snippet: FILE* test = fopen(C:\\core.u, w);
Very often when working on an ASP.NET web site, the options View Code and
Very simple question, is there any cloud server enviroments avaliable these days for us
Very basic question: how do I write a short literal in C++? I know
Very simply, what is tail-call optimization? More specifically, what are some small code snippets
Very odd problem as this is working perfectly on our old Classic ASP site.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.