Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Mar 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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 06:09 PM.

  • #2
    Regular Coder
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    375
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •