I need to load an external webpage into a div. I don't want to use an iFrame. And I want this done with plain Javascript. I'm not sure how to go about it.
-
1I don't think that is possible. You may need to use an iframeEric Goncalves– Eric Goncalves2013-03-04 22:01:29 +00:00Commented Mar 4, 2013 at 22:01
-
3Of course it's possible. Ever hear of AJAX?ithcy– ithcy2013-03-04 22:29:11 +00:00Commented Mar 4, 2013 at 22:29
-
1Does this answer your question? Load html contents of a given url and place exactly there (like document.write()) in JavaScriptMichael Freidgeim– Michael Freidgeim2020-07-16 07:59:05 +00:00Commented Jul 16, 2020 at 7:59
4 Answers
With difficulty…
Use Ajax (e.g. via XMLHttpRequest) to get the page. Since it is external, you will need to bypass the same origin policy.
Once you have the page, extract the relevant parts of it (probably the children of the body element) and add that to your existing DOM.
You'll need to account for differing stylesheets between your site and the external one, for relative URIs (to resources on the external site that aren't on yours), and for any scripts in the remote content.
2 Comments
Whichever method you have, in js, try this instead : $('#myid').load('mylink.com')
I know only this in js.
2 Comments
You don't even need javascript-
but the same restrictions apply as for iframe inclusion of different domains.
<!doctype html>
<html lang="en">
<head>
<meta charset= "utf-8">
<title>test page</title>
</head>
<body>
<div>
<object type="text/html" data="http://validator.w3.org/" width="800px" height="600px" style="overflow:auto;border:5px ridge blue">
</object></div>
</div>
</body>
</html>
1 Comment
You should be able to load html pages within a page using the jQuery.get() method. I'm using this method for two websites for my clients, where the individual pages are different sections of the site.
I'm unsure of the behavior you may encounter if you attempt to use this method loading full HTML pages that include header information and the body tag. I recommend using it to load HTML snippets.