Click here to Skip to main content
15,878,959 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Whenever an ajax post is made, a full page with html 4 doctype is returned but I want to display that page on the current one. There are also some scripts in the head tag of that returned page that are wrapped in document.ready, which means my options are limited to $.parseHTML i.e. they need to be executed whenever the new document is fully loaded. Below is the current code I'm using

What I have tried:

JavaScript
$.post('tet.php', {key: JSON.stringify(payload)}, function(res) {

// res truly returns a full page
 var newPage = $.parseHTML($.trim(res), null, true).find(function (e) {
 try {
  return e.matches('#container'); // this is the one tag I'm interested in
 }
 catch (e) {
  return false;
 }
 });

 $('body').replaceWith(newPage); // errors originate from this line
});


Now the code above keeps throwing all sorts of errors revolving around lack of documentFragment or missing clientWidth etc. How do I go about picking out that element and loading into the DOM while still being able to execute the script in its head tag? If that isn't possible, then how can I remove the current document and install the returned one?
Posted
Updated 29-Jun-17 0:28am

1 solution

"Whenever an ajax post is made, a full page with html 4 doctype is returned but I want to display that page on the current one."

If you wish to display one HTML page within another, I suggest you use an <iframe> element. HTML iframe tag[^]

"Dumping" one HTML page inside of another is bound to cause problems as improperly placed tags abound (two bodies, two headers, two HTML declarations and another DOCTYPE) - all of these out-of-place.
 
Share this answer
 
Comments
nmeri17 29-Jun-17 7:46am    
I know. That's why I'm trying to sift off other nodes in the response while preserving the scripts and executing them once that desired element is loaded into the document. If that doesn't work, my other alternative is to replace the current HTML (and the running script) with the response HTML to take things up from there.
W Balboos, GHB 29-Jun-17 7:49am    
Then why not use a form - it will replace your current page with the target page?
nmeri17 29-Jun-17 7:53am    
You mean returning a form in the response instead of the full page? If I had control over what was returned, I'd even have simply returned an object or JSON or something easier to handle. But it's a full page returned and the page content I need changes after that responses is loaded i.e. after document.ready
W Balboos, GHB 29-Jun-17 7:56am    
No - instead of AJAX put your page's data in a <form>. Process it on the server to build your new page and then it will replace the current page.

You form's SUBMIT button launches to the page designated for "ACTION"
nmeri17 29-Jun-17 8:01am    
Ok. Give me a sec let's see how that works out.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900