PDA

View Full Version : Loading content into iFrames dynamically.


Lefteris
05-10-2003, 11:00 AM
I'll try to make it simple.
On a site that I'm currently building I use a lot nested iframes. In my home page, for example, there is a big container iframe ("main") that loads in it two other iframes ("maintop", "mainbottom"). I have links on the home page that are used to load different pages in the contained iframes. I also have a link that loads content on parent iframe ("main"). When this happens, the two contained iframes disappear, so I cannot access them anymore, unless I get back to the original layout.
In addition an error message appears to inform me that the targed iframes are not present.
I had to devise a way to make these links functional even when the original iframes are not present. What I did was to use a function and the OnClick event load the content on the iframes:
<a href="#" onClick="LinkTo('test.html')">
In the body section of my page I added:
<script language="JavaScript" type="text/JavaScript">
// frame link toggle
var m = "on";

function LinkTo(k) {
if(m=="on") {
parent.frames.main.frames.maintop.location = k;
}
else if(m=="off") {
parent.frames.main.location = k;
}
}
</script>
Finaly on the page that loads on the "main" iframe I added:
<body OnLoad="parent.frames.top.m = 'off';">
As you can see, the solution that I came up was to create a variable accessible from any frame. So it is possible to set its value according to frames loaded, and thus use the correct iframe name to load my content. The script works on my windows machine with no problem but...
Since I'm not a programmer I think that probably there is a simpler and more elegant approach than the one i used.
Also, I'm not so sure that this will work on any browser/o.s. combination.