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 9267847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:39:17+00:00 2026-06-18T14:39:17+00:00

I have a webpage that makes an AJAX request to a PHP script. That

  • 0

I have a webpage that makes an AJAX request to a PHP script. That PHP script responds with a valid JSON object, and sets the Content-type header to application/json.

The JSON format is as follows (as reported by console.log(JSON.stringify(data)) where data is the JSON response):

{
    "content": "<script type=\"text/javascript\">console.log(\"test\");</script>"
}

I then create a div with the class of “content” (remember data is my AJAX responseText JSON object):

var content = document.createElement("div");
content.setAttribute("class", "content");
content.innerHTML = data.content;

Finally, I append the content div to an existing div on my site.

existingDiv.appendChild(content);

I examine the source in the elements tab of Google Chrome’s developer tools because it does a good job of showing what is an actual DOM node, and what is just text. The script block shows up as a DOM node.

Unfortunately, the script is not executed – console.log("test"); does not output test to the developer tools console.

The only option I’ve seen so far is to use eval() which I’m trying to avoid like the plague. Also, my content might contain more than just a script block. It could contain other HTML markup as well.

What am I missing? How can I get this dynamically added block of Javascript to execute?

A jQuery solution is acceptable.

  • 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-06-18T14:39:18+00:00Added an answer on June 18, 2026 at 2:39 pm

    A possible solution is to use new Function() as:

    $(document).ready(function(){
       var script = "window.alert('hi')";
       new Function(script)();
    });
    

    As to why your script is not executing, is what @Pointy suggested (but not entirely + a way to circumvent that), as shown by this code:

    <script type='text/javascript'>
    $(document).ready(function()
    {
        $("<script type='text/javascript'>window.alert('hi');<\/script>").appendTo(document.body); // this will work
    
        var container = document.createElement("div"); // this won't work
        container.innerHTML = "<script type='text/javascript'>window.alert('hi');<\/script>";
        document.body.appendChild(container);
    
        var script = document.createElement("script"); // this will
        script.innerHTML = "window.alert('hi');";
        document.body.appendChild(script);
    });
    </script>
    

    EDIT: added a requested unit test: http://jsperf.com/eithed-hvsa

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

Sidebar

Related Questions

I have a webpage that retrieves data (via ajax/php) and shows it in an
I have a webpage that makes an ajax call to get some data. That
I wish to have a webpage that uses AJAX to access a PHP file
I have a webpage which when it is loaded makes an ajax request to
I have a webpage that is transferring A stylesheet as a text/plain MIME type.
I have a webpage that redirects to another webpage like this: http://www.myOtherServer.com/Sponsor.php?RedirectPage=http://mylink.com/whereIwasgoingtogo.html Then the
I have a simple throbber, that is automatically shown when an ajax request lasts
I have a webpage that receives AJAX data with values listing_id, lat, lng using
I have the following ajax request that creates a link that, when clicked, replaces
I'm making a webpage with dynamic content that enters the view with AJAX polling.

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.