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

  • Home
  • SEARCH
  • 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 510125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:04:53+00:00 2026-05-13T07:04:53+00:00

I have some code where a <script type=text/javascript> block is dynamically inserted. This block

  • 0

I have some code where a <script type="text/javascript"> block is dynamically inserted.

This block contains a function, which has an <input type="button"> element above it (also dynamically inserted) call it with the onclick attribute.

However, it is not working, and Firebug says that the function is not defined when I attempt to click the button.

Is this expectable, and if so is there a workaround?

  • 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-13T07:04:53+00:00Added an answer on May 13, 2026 at 7:04 am

    If you’re writing it to an innerHTML property, it won’t work except for in IE when the DEFER attribute is present:

    scriptParentNode.innerHTML = '<span/><script defer="defer" type="text/javascript">blah();</script>';
    

    Your options are, a) use the DOM to create the element and append it:

    var head = document.getElementsByTagName("head")[0];
    var sTag = document.createElement("script");
    sTag.type = "text/javascript";
    sTag.text = "blah();";
    head.appendChild(sTag);
    

    or b) use document.write at the time your HTML is parsed (but not after!)

    <head>
      <script type="text/javascript">
        document.write('<script type="text/javascript">blah();</script>');
      </script>
    </head>
    

    EDIT

    Seeing as I was downvoted apparently for the information regarding the defer attribute being incorrect, I feel it necessary to post a working example from the MSDN documentation. Whilst it is true that IE removes NoScope elements from the beginning of an innerHTML string, it’s possible to work around this by adding a scoped element to the beginning of the HTML string (example updated above):

    <HTML>
    <SCRIPT>
    function insertScript(){
        var sHTML="<input type=button onclick=" + "go2()" + " value='Click Me'><BR>";
        var sScript="<SCRIPT DEFER>";
        sScript = sScript + "function go2(){ alert('Hello from inserted script.') }";
        sScript = sScript + "</SCRIPT" + ">";
        ScriptDiv.innerHTML = sHTML + sScript;
    }    
    </SCRIPT>
    <BODY onload="insertScript();">
        <DIV ID="ScriptDiv"></DIV>
    </BODY>
    </HTML>
    

    Feel free to actually click the “Show me” button in the MSDN documentation for a working example. [link]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright, to start with let's look at some code: <html> <body> <script type=text/javascript> function
I have this code <title>Welcome</title> <head> <script type=text/javascript> var id = ...; var sessionCount
I have simple HTML code with some JavaScript. It looks like: <html> <head> <script
I have some code below: <!doctype html> <html lang=en> <head> <meta http-equiv=Content-Type content=text/html; charset=utf-8
So I have some javascript code that sends data to my controller: Javascript: <script
I have some Perl code that executes a shell script for multiple parameters, to
I have the following perl script: $myVariable = some value; //... some code ...
I have java script code to set some of the properties of ajax controls.
I have an SGE script to execute some python code, submitted to the queue
We have a system that has some Bash scripts running besides Java code. Since

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.