Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

fearless3955

macrumors newbie
Original poster
Sep 12, 2008
8
0
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()">
 
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()">
 
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.
 
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.

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.
 
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...?
 
I wonder if the individual gifs that make up the composite animated gif can each have their own embedded urls instead...?

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
 
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!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.