PDA

View Full Version : how to get name of iframe parent page



pdsok
11-23-2007, 11:51 AM
I am using the following for links in an iframe menu

<a href="home.htm" target="_top"
onmouseover="document.home_but.src='images/homeon.gif'"
onmouseout="document.home_but.src='images/homeoff.gif'">

<IMG SRC="images/homeoff.gif" width="142" height="27" border="0"
name="home_but">

</a>

I want to make it so that the 'on' image is used if the parent page is already loaded but the 'off' image is used if the parent is some other page

I've tried this and variations like it but I can't get it right

<a href="home.htm" target="_top"
if (parent.location.href=home.htm)
{<IMG SRC="images/homeon.gif" width="142" height="27" border="0"
name="home_but">}
else
{onmouseover="document.home_but.src='images/homeon.gif'"
onmouseout="document.home_but.src='images/homeoff.gif'">

<IMG SRC="images/homeoff.gif" width="142" height="27" border="0"
name="home_but">}
</a>

any help would be appreciated
thanks
PdS

molendijk
11-23-2007, 12:43 PM
You need to know whether or not the parent page has loaded?

See:
http://www.sitepoint.com/blogs/2004/05/26/closures-and-executing-javascript-on-page-load/

Arie Molendijk.

pdsok
11-25-2007, 09:02 PM
no, what I need is really a cosmetic fix,

It is to show which page is the parent page if it is one of the menu pages so that the correct image loads to indicate that menu option is already live.

The menu loads an 'off' image and onmousover shows an 'on' image, I want to show the 'on' image if that page is already loaded, the menu is in an iframe loaded into each page.

I think it was you that helped me with the menu in the past,

the page is here tstiger.org.uk

sorry i did not answer sooner, very busy weekend :)

thanks
PdS

jscheuer1
11-25-2007, 09:21 PM
Looks like you already have some fancy stuff going on. Anyways, the name of the parent, mmm.

I think you actually mean the filename of the parent. These sorts of things must be done all on one domain. Given that (which it appears you have):


parent.location

contains the entire address of the parent page. If you need to parse that, and don't know how, let me know.

Also useful might be:


top.location

That's for when there may be several levels of frames and/or iframes. It will report the top level. Parent only goes up one level from the current page.

Both will report the current page if there is no containing frame.

To get an idea of what is being returned, paste this into the address bar and hit enter:


javascript:alert(parent.location.href)

and then try:


javascript:alert(parent.location.href.replace(/.*\//,''))

if that gets you what you are looking for, use:


parent.location.href.replace(/.*\//,'')

as the basis for comparison.

pdsok
11-26-2007, 05:59 PM
Thanks John, that works a treat but . . .
Below is the script I would like to use, as you can see I am setting the variable 'parentid' using the code you supplied and then using an if then,
however I cannot get it right for the document.write parts.

It will work with a basic document.write 'yes' or 'no', or to load an image etc but as soon as I add the onmousover it stops working.

I am sure it's something to do with the " or ' in the onmouseover parts but I am really struggling


<script type="text/javascript" language="javascript">
var parentid = parent.location.href.replace(/.*\//,'')
document.write ('hello')
document.write ('<br>')
document.write ( parentid )
document.write ('<br>')
document.write ('or')
document.write ('<br>')

if (parentid=='testpage.htm')
{
document.write('<a href='testpage2.htm' target='_top'><img border='0' src='homeoff.gif' onMouseOver='src='homeon.gif'' onMouseOut='src='homeoff.gif'' width='142' height='27'><\/a><br>'')
}
else
{
document.write(''<a href='testpage2.htm' target='_top'><img border='0' src='homeon.gif' width='142' height='27'><\/a><br>'')
}

</script>



I should add that I am not normally a script guy and I usually make do with prewritten scripts then figure out how they work and change bits to suit, but with regard to the correct syntax I often struggle :rolleyes:

The webpage started out as a favour to our local Sea Cadets as I knew a bit about html but it is now becoming something of a monster :)
still good fun tho'

Thanks in advance
PdS

jscheuer1
11-26-2007, 06:53 PM
You need a crash course in nested quoting, but I'll just give you back your corrected example, hopefully that will give you the idea:


<script type="text/javascript">
var parentid = parent.location.href.replace(/.*\//,'');
document.write ('hello');
document.write ('<br>');
document.write ( parentid );
document.write ('<br>');
document.write ('or');
document.write ('<br>');

if (parentid=='testpage.htm')
document.write('<a href="testpage2.htm" target="_top"><img alt="Home" border="0" src="homeoff.gif" '+
'onmouseover="this.src=\'homeon.gif\';" onmouseout="this.src=\'homeoff.gif\';" width="142" height="27"><\/a><br>');
else
document.write('<a href="testpage2.htm" target="_top"><img alt="Home" border="0" src="homeon.gif" width="142" height="27"><\/a><br>');

</script>

Notes: The language attribute has been deprecated, brackets are not required when there is only one thing being done as the result of a condition. A semicolon is a good idea at the end of an action statement. I split the one line of document.write, using a close quote +, and then reopening the quote on the following line because we can, and it is easier to read that way. The 'this' keyword is a good idea in the two places where I added it.

pdsok
11-26-2007, 07:48 PM
Many many Thanks for that John, looking at it I don't think I would've got there in a hurry, as you say I could do with a bit more than a crash course.

My intention is to repeat that code six times to build the menu that you saw on the left of the TS Tiger page.

I realise that it is only cosmetic but I think it improves the feel of the menu.

again many thanks my friend

regards
PdS
reluctant webmaster
tstiger.org.uk