user3213490 user3213490 - 2 months ago 4x
HTML Question

Calling javascript synchronously after making ajax call for the new content

I am using ajax call to refresh the div content from a new jsp. there are some tags which I am calling asynchronously through call which is why they are not working. ( tags are not working). how do I make them synchronized while displaying.

My idea is through document.write(code). But its not working out. Can any body suggest a solution to this please.


You can't use document.write() in an Ajax callback or in any asynchronous callback. You can only use it during the initial page load. If you call document.write() after the page has loaded (e.g. in an Ajax callback) it will wipe the entire page contents before writing the data.

If you're using jQuery, the load() method is an easy way to fetch data from a server and insert into an element on your page. An example from the load() documentation:

$( "#result" ).load( "ajax/test.html" );

That will download the ajax/test.html page from your server and insert its content into the element whose id is result.

load() is a convenience wrapper around the $.ajax() function. For more options, error handling and such, you can call $.ajax() directly, as in the example below. This should give similar results to the above when it succeeds, but it writes an error message if the server call fails:

    url: "ajax/test.html"
.done(function( html ) {
    $( "#result" ).html( html );
.fail(function( jqXHR, textStatus ) {
    $( "#result" ).html( "Request failed: " + textStatus );