Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-19-2006, 05:06 PM   PM User | #1
charlottedee
New to the CF scene

 
Join Date: Mar 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
charlottedee is an unknown quantity at this point
Changing source of iframe

I'm using an iframe within my webpage and want to have a link that when clicked changes the content of the iframe, however, each time it is clicked it will link to a different page from a javascript array. At the moment I have tried a function which increments the position in the array and should then change the content of the iframe but this doesn't work. I've tried doing this using the dom model rather than linking with target as I couldn't handle the array that way.


function next(){
pos+=1;
item=regionarray[pos];
document.getElementById("info").src="./page.php?item="+item;
}

<IFRAME SRC="./top.html" name="info" WIDTH=450 HEIGHT=100>
If you can see this, your browser doesn't
understand IFRAME. However, we'll still
<A HREF="./top.html">link</A>
you to the file.
</IFRAME>
<p>
<a href='javascript:next()'>Next</a>


Does anyone know why its not working with the DOM stuff or whether there is a better way of doing this?

Thanks

Last edited by charlottedee; 03-19-2006 at 05:09 PM..
charlottedee is offline   Reply With Quote
Old 03-19-2006, 07:40 PM   PM User | #2
jscheuer1
Regular Coder

 
Join Date: Mar 2005
Location: SE PA USA
Posts: 373
Thanks: 0
Thanked 0 Times in 0 Posts
jscheuer1 is an unknown quantity at this point
Iframes are a bit tricky. Ordinarily your code might work in IE. However, for all other browsers, the script engine will never retrieve an element by id if it doesn't have that id. In your example you are getting the iframe by id but it is its name, not its id that is set to 'info'. Additionally, as I say, iframes are tricky so even though IE will often retrieve a named element by id, even it probably won't in this case. To make matters worse, iframes don't usually respond well to having their src attribute changed in this fashion. A better approach would be:

Code:
window.frames.info.location="./page.php?item="+item
This will access the iframe by name and load the new address into it.
__________________
- John
jscheuer1 is offline   Reply With Quote
Old 03-20-2006, 09:04 AM   PM User | #3
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
or give in an id as well , isn't that easier?

<IFRAME SRC="./top.html" name="info" id="info" WIDTH=450 HEIGHT=100>

Now you may use whichone of the folowing

by name
top.['info'].location.href=newurl
by id
document.getElementById('info').src=newurl
document.getElementById('info').setAttribute('src',newurl);
document.getElementById('info').contentWindow.location.href=newurl;
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:06 AM.


Advertisement
Log in to turn off these ads.