Suppose we have a document index.php
<!DOCTYPE html> <html> <head> <meta charset='cp1251'> </head> <body> документ 1 </body> <script src='script1.js'></script> </html>
And the second document is next to new.php
<!DOCTYPE html> <html> <head> <meta charset='utf-8'> </head> <body> документ 2 </body> <script src='script2.js'></script> </html>
In the first script, we execute the query, get a new document, and upon receipt we simply replace all the content. Here are just the scripts and styles will be loaded separately
xhr = new XMLHttpRequest(); xhr.open('GET', 'new.php', true); xhr.send(); xhr.onreadystatechange = function() { if(xhr.readyState == 4 && xhr.status == 200) { document.write(xhr.responseText); } }
Upon receiving the answer, you can temporarily write it to the element of the current document. Then search for pictures, scripts, styles and recursively load them.
if(xhr.readyState == 4 && xhr.status == 200) { tmp = document.createElement('div'); tmp.style.display = 'none'; document.body.appendChild(tmp); tmp.innerHTML = xhr.responseText; resources = Array.from(tmp.querySelectorAll('script, img, link')); resources.forEach(function(e, i) { link = e.src || e.href; console.log(link); }); // пройтись по массиву resources и всё загрузить }
When resources are loaded, execute document.write(xhr.responseText);
iframedoes not fit, you just need to replace the content of the document. Shadow DOM is interesting. Did not even know about this technology. - user194374