Different methods of inserting external HTML in to a document

Here are a few examples for inserting an external html file into a document. This article is only a stub until I can develop it further.

Inserting via javascript (jquery):

Very easy, especially if you are already using the jquery library. More information of how this method works can be found on the jquery site. This method uses GET/POST methods and will NOT allow you to insert content from an external domain. Still searching for a method that allows cross-domain content that also works in most browsers.

In the document head:


<script type="text/javascript" src="jquery.js">

</script>

<script type="text/javascript">

$(document).ready(function(){

$(function () {

$("#targetDiv").load("data.html");

});

});

</script>

and in the body:


<div id="targetDiv"></div>

Ajax Version Example

Inserting via OBJECT tag:

Similar to an Iframe. Spotty support across different browsers such as you can NOT control the border property in IE so you get the ugly scrollbar even if your content does not exceed the size of the object. Conditional is required for IE since it needs the classid attribute. More information about the object tag can be found here and here.


<!--[if IE]>

<object 

classid="clsid:25336920-03F9-11CF-8FD0-00AA00686F13" 

data="data.html" 

style="height:400px;width:400px;">



<p>Data was not fetched</p>

</object>

<![endif]-->

<!--[if !IE]> <-->

<object type="text/html" 

height="400px" width="400px" 

border="0" data="data.html" 

style="height:400px;width:400px;border:0; ">

<p>Data was not fetched</p>

</object>

<!--> <![endif]-->

Object Version Example

Inserting via iframe:

Supported in most browsers but is not supported with strict DOCTYPEs. Access to external domains available per testing in IE7 and FF3.


<h2>Yahoo Iframe</h2>

<iframe src="http://www.yahoo.com" 

style="height:400px;border:0;">

No iframe support</iframe>

<p>End Yahoo</p>



<h2>Local data.html</h2>

<iframe src="data.html" 

style="height:400px;border:0;">

No iframe support</iframe>

<p>End local</p>

Iframe Version Example

embedding external html

I'm very new to creating websites so I'm crawling to find my way. I find your website helpful. Thanks. It helped me save a lot of work. I still have a problem: I have a java based dropdown menu on all pages + an accordeon menu on my portfolio page to select from my works. I'm trying to keep these navigation areas external, inorder to be able to make changes easily. (Because I have more than 30 works and I want the works on the same page with the navigation areas- I also don't want to use frames- so I had to place the navigations on each work's page. If I need to change any of the navigation items I have to change it in more than 30 pages. If I could only place the navigation areas from an external html file inside a div tag, it would save a ton of work.) I used your sample with jquery (I can say I know nothing about html and java literally ) I ve placed a menu field from an external html file. It worked very well, but accordeon menu did not. If I place the accordeon code between div tags of main html, it works, but from external files it doesn't - I need your help - I must be doing something incorrectly.) I didn't want to send several lines of code here. Is it ok to send you by e mail? Thanks

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Lines and paragraphs break automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

Verify Your Humanity
This question is for testing whether you are a human visitor and to prevent automated spam submissions.