/    Sign up×
Community /Pin to ProfileBookmark

Replace entire html page with ajax response

Hi all,

I want help with a problem that I am dealing with. I ‘m having an ajax function that polls a server for a response. This response is in fact an html page which is dynamically constructed on the server. What I want is to be a able to replace my current html page ( not only the body ) with the html I am receiving as response from the server. I tried using document.write or innerHTML on html element but none of these does not seem to work. Here is a sample of the code I am using.

[code=php]
function ajaxCall()
{
var actionPath=window.location.href;

jQuery.ajax({
url: actionPath,
type: ‘GET’,
dataType: ‘html’,
error: function(){
alert (“ERROR”);
},
success: function(resp,textStatus){
document.getElementsByTagName(‘html’)[0].innerHTML = resp
}
});
}

jQuery(function() {
myInterval = setInterval(“ajaxCall()”, 10000);
});
[/code]

If you have any suggestions I will be glad to hear.

Thanks.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@Shanu_chaudharySep 21.2010 — Just try to place your html page inside a specific div with and ID and then replace it with the response text.

Whether your response text is an entire html file with the <html> tags???? If so this wont be possible.
Copy linkTweet thisAlerts:
@ypanagioauthorSep 21.2010 — Unfortunately the response text is an html page and not only the body of the html page. Nevertheless, i found some kind of workaround for my problem, though not very pretty it seems to do the job. Here is a snippet of the code I wrote
[CODE]
success: function(resp,textStatus){
var startHead = resp.indexOf("<head>");
var endHead = resp.indexOf("</head>");

var headString = resp.substring(startHead+6,endHead);

var startBody = resp.indexOf("<body>");
var endBody = resp.indexOf("</body>");
var bodyString = resp.substring(startBody+6,endBody);

jQuery('head').html(headString);
jQuery('body').html(bodyString);

/*Here function that appends js scripts to the head after creating script elements*/
...
});
[/CODE]
Copy linkTweet thisAlerts:
@rnd_meSep 21.2010 — window.open(); document.write(responseText) should work...
×

Success!

Help @ypanagio spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...