Open a link within an iFrame

Discussion in 'JavaScript' started by mindgem, Sep 19, 2009.

  1. #1
    Hello folks! I don't know anything about JavaScript but I found a really cool snippet I use a lot. It will randomize images and each image can open a new link. It looks like this:

    ----------------------------------------------------
    <script language="JavaScript" type="text/javascript">

    function random_imglink(){

    var myimages=new Array()

    myimages[1]="/img/images1.jpg"
    myimages[2]="/img/images2.jpg"
    myimages[3]="/img/images3.jpg"

    var imagelinks=new Array()

    imagelinks[1]="http://www.google.com"
    imagelinks[2]="http://www.yahoo.com"
    imagelinks[3]="http://www.alexa.com"

    var ry=Math.floor(Math.random()*myimages.length)
    if (ry==0)
    ry=1
    document.write('<a href='+'"'+imagelinks[ry]+'"'+'><img src="'+myimages[ry]+'" border=0></a>')
    }

    random_imglink()
    //-->
    </script>
    -------------------------------------------------

    Now, my question is, can I link to open a document within an iFrame on my page?

    Please don't tell me not to use iframes or whatever, I'm not a professional, just want to see if this works.
     
    mindgem, Sep 19, 2009 IP
  2. SubZtep

    SubZtep Member

    Messages:
    69
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    48
    #2
    Sure. Add name into the iframe and target into the link (these must be the same).
    For example:

    <a href="#" target="myframe">...</a>

    <iframe name="myframe"></iframe>
     
    SubZtep, Sep 19, 2009 IP
  3. mindgem

    mindgem Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3

    Hiya SubZtep.

    I tried with:
    var imagelinks=new Array()
    imagelinks[1]="<a href="/page1.htm" target="MyiFrameName">...</a>"

    but it doesn't work. I'm sure this can work with the proper syntax.
    Just to lcearafy, I know how to do it using html but I want to know if it's possible with JavaScript as with my code snippet up there.

    Care to modify it to make it work?
     
    mindgem, Sep 19, 2009 IP
  4. SubZtep

    SubZtep Member

    Messages:
    69
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    48
    #4
    if you add id value for the anchor you can setup the target with javascript like this:
    document.getElementById('linkid').target="framename";


    Maybe you just have syntax error, try this:

    imagelinks[1]='<a href="/page1.htm" target="MyiFrameName">...</a>';
    or
    imagelinks[1]="<a href=\"/page1.htm\" target=\"MyiFrameName\">...</a>";
     
    SubZtep, Sep 19, 2009 IP
  5. mindgem

    mindgem Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    ok, another explanation.

    I have my index .htm. On that I have 1 iFrame where I load these random images and another iframe called iFrameName. I want to load google.com
    in the iFrameName iFrame when clicking on the random image.

    imagelinks[1]=<a href="sida1.htm" target="iFrameName"></a>
    imagelinks[2]="sida1.htm" target="iFrameName"
    imagelinks[3]="http://www.alexa.com"

    I tried these,,,doesn't work.
     
    mindgem, Sep 19, 2009 IP
  6. mindgem

    mindgem Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The link you showed me would work if the image link were on the same page as the iframe, but the link is inside an iframe trying to open content in another iframe on index....

    I know this is probably very wrong in every way but still...is it possible to fix that link so it goes something like
    target="index.htm#iFrameName"
     
    mindgem, Sep 19, 2009 IP
  7. SubZtep

    SubZtep Member

    Messages:
    69
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    48
    #7
    Sorry, I don't really understand exactly the problem, but you can modify any frame source with javascript.

    For example <a href="javascript:framename.location.href = 'xxx.html'">...</a>
    or you can navigate between different windows like window.opener.location etc...

    Maybe helpful if you are post your whole code...
     
    SubZtep, Sep 19, 2009 IP
  8. ThomasTwen

    ThomasTwen Peon

    Messages:
    113
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #8
    he basically has a page with two iframes. When he clicks on something within one iframe, he wants something to be loaded in the other iframe.


    This is, however, not possible, because iframes do not have an onclick() event - and anything triggered from within the iframe is not able to access contents of the second iframe.



    Does the random image have to be in an iframe? Just put the random image on your main page (index.php), and use
    
    <script type="text/css">
    function myfunction(url) {
    document.getElementById("myframe").src = url;
    }
    </script>
    <img onclick="myfunction(google.de);">
    Code (markup):
     
    ThomasTwen, Sep 26, 2009 IP