How do I open NEW random window with this javascript?

Discussion in 'Web Design and Development' started by fearless3955, Sep 12, 2008.

  1. fearless3955 macrumors newbie

    Joined:
    Sep 12, 2008
    #1
    I'm using the script below to open a random page on clicking the relevant image but want it to open a NEW window instead of changing the current one. Any ideas? Do I change the html or the javascript? If so, how?
    1. SCRIPT:
    <script type = "text/javascript">
    <!--
    function random_all(){
    var myrandom=Math.round(Math.random()*(document.links.length-1))
    window.location=document.links[myrandom].href
    };//--></script>

    2. HTML:
    <a href="#" onMouseOver="MM_swapImage('Image57','','images/splash/images/splash_anim.gif',1)" onMouseOut="MM_swapImgRestore()"><img src="images/splash/images/splash_09a.jpg" name="Image57" width="422" height="261" border="0" onclick = "random_all()">
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
  3. fearless3955 thread starter macrumors newbie

    Joined:
    Sep 12, 2008
    #3
    Doesn't work right

    I tried this but it makes the ORIGINAL page change to the random new page, while opening a NEW window in front that's the same as the one I just left. In other words it's all happening back to front! What am I doing wrong?
    This is how that bit of amended code now reads:
    <a href="#" target="_blank" onMouseOver="MM_swapImage('Image57','','images/splash/images/splash_anim.gif',1)" onMouseOut="MM_swapImgRestore()"><img src="images/splash/images/splash_09a.jpg" name="Image57" width="422" height="261" border="0" onclick = "random_all()">
     
  4. fearless3955 thread starter macrumors newbie

    Joined:
    Sep 12, 2008
    #4
    2nd reply

    I see you've amended you reply during my own first reply, but your 2nd suggestion won't work changing the js this way because it's not a single location - it's a random script.
     
  5. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #5
    Yea I misread your post on first read so was thinking along the wrong lines and went back and changed it.

    For clarification, on my suggestion did you try?:
    PHP:
    window.open(document.links[myrandom].href);
    Because that would handle the random component, at least if I'm understanding your script correctly.
     
  6. fearless3955 thread starter macrumors newbie

    Joined:
    Sep 12, 2008
    #6
    Yes, thank you. This way I now end up with 3 windows - and still the new (random) one I want as my front window changes behind (obscured by the new one that opens at my original url)... so, on the face of it, it looks to the visitor like the link leads nowhere. I think maybe I need to search for a different random script, though this one was good because it's so simple and doesn't need me to spell out the many urls.
    I've reverted to a single fixed link in the meantime, but if you have any ideas... it's here: http://www.valrutt.com/
    (...it's the rollover on the pictured PC screen that I want eventually to be random). I wonder if the individual gifs that make up the composite animated gif can each have their own embedded urls instead...?
     
  7. SrWebDeveloper macrumors 68000

    SrWebDeveloper

    Joined:
    Dec 7, 2007
    Location:
    Alexandria, VA, USA
    #7
    I am not aware animated gif's can have a unique link per individual frame. The usual approach is to use Javascript (DHTML) or Flash to embed images of any format as individual frames, each frame having it's own time delay and optional link. Think of it as a mini slideshow or rotating sponsor advert or banner. Plenty of those available on the Internet!

    -jim
     
  8. fearless3955 thread starter macrumors newbie

    Joined:
    Sep 12, 2008
    #8
    Random page in separate window now works!

    Problem sorted. I now have my random page(s) opening in a separate new window in front... though I haven't a clue why it now works or even if it works properly on anything other than a Mac/Safari... but if anyone needs to see/use the working code:
    http://www.valrutt.com and just roll over the PC screen image where this whacky little function is located.
    Thanks chaps!
     

Share This Page