Forum Moderators: open
Say I have an index page (index.html) and I have another page featuring an iframe (iframe.html) which of course has a default source page (default.html).
What I want to do is put a link on index.html that takes the user to iframe.html, but instead of displaying default.html in the iframe, we substitute another page to appear (alternate.html).
Can this be done? I tried searching, but came up confused and empty-handed.
So, one easy way that I've done it is to add parameters to the anchor on the index.html page.
<a href = "iframepage.html?newsrc=newpage.html">text</a>
Then, onload of iframepage.html, check to see if there is a param (there is "?newsrc=newpage.html") and if so, change the iframe src to newpage.html.
I'm pretty sure there are actual code snippets on this forum about doing just this sort of thing cause I've posted some before. Search for onload and location.href. If you can't find it, lemme know and I'll find it to post again.
Step 1: Anchor href (assumes changeme is iframe name
<a href="somedir/iframepage.html?changeme= ../nutherdir/newiframesrc.html" target = "_top">some anchor text</a>
Step 2: Iframe page onload
<body onload = "iframe_src()";>
Step 3: Onload function
function iframe_src()
{
var locsubstr = location.search.substring(1);//find url parameter
if((locsubstr.indexOf("changeme")!= -1))//if there is a new path for changeme
{
var changemeloc = locsubstr.substring(locsubstr.indexOf("=") + 1, locsubstr.length);//find the newpath for changeme
parent.frames[0].location.replace(changemeloc);//change changeme to new path
}
else
{parent.frames[0].location.replace(defaultpath);}//if there isn't a new path, set changeme to default
}
So, in the head of the iframe page, you'd have:
<SCRIPT LANGUAGE="JavaScript" SRC="somedir/external.js" type = "text/javascript"></SCRIPT>
And then the body would have the onload:
<body onload = "change_iframe_src()">
Mind you, I forgot what I called the function that I posted earlier, just make it match that.