View Full Version : DOM location of iframe

07-26-2004, 03:00 AM
right, ive got my main page with an iframe on it. i want to change a div layer visibility depending on the location of the iframe (ie the url). im guessing i need to use DOM to find the location but im not too sharp on the ol' javascript! if someone could help me out id be v. grateful.
Also, how can i get it too update if the location of the iframe changes?

07-26-2004, 06:35 PM
Well if you assign the iFrame an id, then you can use document.getElementById() to retrieve it:

<iframe id="id_name" ...></iframe>


var i_frame = document.getElementById('id_name');

So whatever id you assign to the iFrame, you need to use the same id to retrieve it using getElementById().

Let me know how it goes,

07-27-2004, 01:00 PM
yeh ive done that. what ive got is


with content being the iframe id.

but how do i find the src or location of the iframe? ie which page is displayed?

07-27-2004, 01:01 PM
whoop, sorry i didnt read your post properly (a little drunk!) thanx mate, ill give it a whirl

07-27-2004, 01:25 PM
yeh works a treat. but the problem is, how do i get it torefresh the variable once the page has changed inside the iframe?

heres the bigger picture if it helps: ive got a nav bar, pointing at the iframe. ive then got a logo on the main page (same page as the nav bar) and my client wants the logo to change depending on the page displayed in the iframe! &$@*%!!! is there an easier way to go about this, or am i heading down the right road with javascript?

07-27-2004, 06:30 PM
Yes you can do this with Javascript.. depending..

So when will the page within the iFrame get changed? Only when an item from the nav bar is selected, correct? Are there links in the pages within the iFrame? It might be tricky (I'm not sure if there's an event of some kind that you can handle for this..) if the page in the iFrame can change with the user needing to click on the main frame.

If this is the case, it may be better to scrap the iFrame idea, and move to using a DIV and server side using includes (using PHP or something...). This will require the page to 'refresh' more, but you'll have better control over it.