...

View Full Version : Loading a certain page in an iframe by identifying the bookmark via javascript



unbreakable
06-30-2004, 07:45 AM
Hi, i've a site and i would like to load a particular page in an iframe when a certain bookmark is in the location bar. What i came up with is the below code, but obviously it doesn't work.

<script language="javascript">
var isrc1 = window.location.href.indexOf("test");
if ( isrc1 > 0)
{
alert(window.location.href)
frames['myframe'].location.href = "test.htm"
}
</script>

What i'm trying to do is, when the url is "http://www.mysite.com/index.htm#test", load test.htm in an iframe called myframe in index.htm.

Hope someone can help me :)

glenngv
06-30-2004, 08:04 AM
Using location.hash will return just the anchor name including the #.

if (location.hash=="#test")
{
window.frames['myframe'].location.href = "test.htm";
}

You can put that in a function and call it onload so that you're sure that the iframe has loaded before you call it.

unbreakable
06-30-2004, 08:32 AM
ok, using your code, i added this in :)

<script language="javascript">
function loader()
{
if (location.hash=="#test")
{
window.frames['myframe'].location.href = "test.htm";
}
}
</script>

and also added this to the iframe:

<iframe name="myframe" onload=loader()>

however when i load index.htm#test, the iframe will continually load a page w/o stopping. am i doing anything wrong? thanks :)

glenngv
06-30-2004, 08:52 AM
You have an endless loop. And besides iframe onload is supported in IE only.
What I meant in my previous post was onload of the main document not of the iframe.

<body onload="loader()">
<iframe name="myframe" src="about:blank"></iframe>
</body>

You may want to load a default page for the iframe so that javascript-disabled browsers can see something in the iframe.

unbreakable
06-30-2004, 08:55 AM
i figured it out just before u posted... lol... :)

thanks anyway for yr help :)

i do have a default page loaded, i didn't add it in the code i posted :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum