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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:56:33+00:00 2026-05-23T17:56:33+00:00

I am requesting with AJAX (using jQuery) a tag which contains a Javascript function

  • 0

I am requesting with AJAX (using jQuery) a tag which contains a Javascript function call to an Apache+PHP server. Here is the HTML and AJAX code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="http://localhost/LivingLab/javascript/jquery.min.js"></script>
</head>

<body>
<a href="javascript: void(0)" onclick="
    $.post(
        'http://localhost/test.php',    // server target page
        {some_var: 'abc'},              // data to be sent via POST method
        function(data) {                // JS callback function
            $('#container').html(data); // innerHTML of <div id="container"> will be replaced
        }
    )
">Click me!</a>
<div id="container"></div>
</body>
</html>

AJAX requests the page ‘http://localhost/test.php&#8217; which has the following PHP code:

<?php
    echo '<script type="text/javascript"> document.write("Javascript output"); </script>';
?>

When I click on the link, after the AJAX request, the whole page is replaced by “Javascript output”, instead of writing this only inside the div tag with id “container”.

If the PHP server script writes something else, instead of a script tag like this:

<?php echo 'text' ?>

the AJAX call behaves normal, by replacing div with id “container” and not the whole page.

I tried to investigate HTTP data transfered by this pages with Wireshark and it doesn’t seem to be an HTTP problem because server response is exactly how it should be:

<script type="text/javascript"> document.write("Javascript output"); </script>

Running the Javascript code without AJAX does not replace the whole page, this happens only when the script tag is returned using AJAX. A noticed the same behavior in Firefox 3 and 5 and also in Google Chrome.

Why does this happen?

  • 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-23T17:56:33+00:00Added an answer on May 23, 2026 at 5:56 pm

    document.write is normally used during the main load of the page, during which it outputs to the parsing stream that the browser reads to render the main page. If you use it after the initial load of the page is complete, it implicitly does an open on the document, which completely tears down the document and starts fresh with the newly-written content.

    The short version is: Only use document.write during main page load (if then). After the page has loaded, use DOM manipulation instead.

    Example (live copy):

    <body>
      <input type='button' id='theButton' value='Click Me'>
      <script>
        document.write(
          "<p><code>document.write</code> is fine here, during " +
          "the main load of the page.</p>");
        document.getElementById("theButton").onclick = function() {
          document.write(
            "<p>But not here, because the initial " +
             "page load is complete and using " +
             "<code>document.write</code> at this point tears " +
             "down the document and starts  new one.</p>");
        };
      </script>
    </body>
    

    More about from the DOM2 HTML spec, the HTML5 spec, and MDC:

    • document.write: DOM2 | HTML5 | MDC
    • document.open: DOM2 | HTML5 | MDC
      (called implicitly by document.write if you call it once the page is loaded)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a feed using jquery ajax and requesting data from facebooks
I am using jQuery.ajax ( http://docs.jquery.com/Ajax/jQuery.ajax ) to inject the contents of a different
I'm using jQuery's ajax methods to interact with the Twitter REST API . Their
I have a simple ajax call: $.ajax({url: my_url_here, dataType: 'text', success: function(data, textStatus) {
When requesting hardware for a WebLogic server, what hardware would best improve its performance?
I have the following script element in my web page: <script src=default.js type=text/javascript></script> Using
I'm very new to jQuery and AJAX. i read a bit of the API
I have an application where the html/javascript code executes fine in a standalone browser
I have a client who is requesting an HTML-formatted email to promote an event.
I often have customer requesting changing properties, like the version history setting on all

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.