How to evaluate HTTP response received in fetch api

How can we evaluate the http response received in the fetch api ( Is there a way to not send cookies when creating XMLHttpRequest in the same source? )?

we access responseText as a result of a fetch call. Now we can programmatically (possibly by calling api on responseText) what the browser does (called page loading), for example, loading images, css files, etc., And evaluating scripts and possibly creating xmlhttprequests, if any .

I want to get the html content on the page when all this is done, i.e. equivalent to when the browser transitions from the download state to β€œcomplete” after all of this has completed.

I hope the question is clear.

+1
source share
1 answer

You can do response.text() in an <iframe> (with visibility: hidden if you want). Then you can manage a new document with all the standard features:

 <!DOCTYPE html> <html> <head> <title>This is the page title</title> <meta charset="UTF-8"> <meta name="description" content="Free Web Help"> <meta name="keywords" content="HTML,CSS,XML,JavaScript"> <meta charset="utf-8"> </head> <body> </body> <script> var img = new Image(); img.src = "http://cdn.sstatic.net/stackoverflow/img/ apple-touch-icon@2.png "; document.body.appendChild(img); fetch("so.html") .then(function(response) { return (response.text()); }) .then(function(responseText) { // limit the recursion depth to 1 if (!window.frameElement) { var newDoc = document.createElement('iframe'); document.body.appendChild(newDoc); newDoc.contentDocument.write(responseText); } }); </script> </html> 

To try it yourself, you can save this code as so.html and access it on your local web server, or you can check here .

+1
source

All Articles