0

I got a html file like this,

<!DOCTYPE html>
 <html lang="en">
  <head>
   //some content
  </head>
  <body>
   //some content
  </body>
 </html>

My question is how to load this file as a whole with jquery. I tried it with append function, but it didn't work.. I searched for the solution for quite a while, but just found a lot of methods to append some parts of html file, like meta, link, not a whole file.

Can I do it with jquery?

Ranger 22
  • 415
  • 4
  • 19

2 Answers2

1

index.html or whatever

<iframe src="filename.html"></iframe>

filename.html

<!DOCTYPE html>
 <html lang="en">
  <head>
   //some content
  </head>
  <body>
   //some content
  </body>
 </html>
Cameron
  • 1,049
  • 12
  • 24
  • OP is trying to replace existing `document` with imported `document`, not display imported `document` in ` – guest271314 Apr 14 '17 at 04:24
  • I thought he was "appending" it which would mean adding it at the end or in a child element etc – Cameron Apr 14 '17 at 04:26
  • See http://stackoverflow.com/questions/43404728/how-to-append-a-whole-html-file-with-jquery/43405004#comment73870071_43404728, http://stackoverflow.com/questions/43404728/how-to-append-a-whole-html-file-with-jquery/43405004#comment73870107_43404728 – guest271314 Apr 14 '17 at 04:31
0

You can use <link> element with rel attribute set to import, href set to path to file, type set to "text/html". Use XMLSerializer() instance .serializeToString() with replacement document .doctype property as parameter to get <!DOCTYPE> declaration from imported document; document.write() with .import.documentElement.outerHTML property of link element as parameter to replace existing .doctype and <html> node with .doctype and <html> of imported document.

<!DOCTYPE html>
 <html lang="en">
  <head>
   <link id="doc" rel="import" href="https://gist.githubusercontent.com/guest271314/9921cb52b143437b23f23fa32284ca35/raw/86532c043b666bce15b33c91dc29e98615dd4e25/replacementDocument.html" type="text/html" />
  </head>
  <body>
   //original content
   <button>load html document</button>
   <script>
   var link = document.getElementById("doc");
   document.querySelector("button")
   .onclick = function() {
     // http://stackoverflow.com/a/30565562/
     var dt = new XMLSerializer().serializeToString(link.import.doctype);  
     document.write(dt, link.import.documentElement.outerHTML);
     document.close();
   }
   </script>
  </body>
 </html>
guest271314
  • 1
  • 15
  • 104
  • 177