Monday, June 16, 2008

JavaScript: Loading an External File to a DIV

this script will let you load an external text file to a DIV container in the html file.

first, put this code in the 'head' section of the html:
<script>
<!--
function ReplaceText(container,content) {
document.getElementById(container).innerHTML = "<iframe src='"+content+"' width=600 height=400 frameborder=0 scrolling=auto>";
}
-->
</script>

and then use this in the link:
<a href="#" onClick="ReplaceText('base','sample.txt')">Load Text</a>

the container div should be something like this:
<div id="base"><div>

happy coding :-)


3 comments:

Unknown said...

Well Done! Keep going ... :)

Unknown said...

Loading the external page into an iframe has 2 downsides:
- the <area alt="text"> tooltips no longer work (at least in IE6) in the iframe'd page
- if 2 pages are loaded like this (one for menu, one for content), the menu cannot drive the content as they are not in the same space.
Still struggling with this...

Unknown said...

Loading the external page into an iframe has 2 downsides:
- the <area alt="text"> tooltips no longer work (at least in IE6) in the iframe'd page
- if 2 pages are loaded like this (one for menu, one for content), the menu cannot drive the content as they are not in the same space.
Still struggling with this...