PDA

View Full Version : How do I make javascript random link generator target a given frame?


Remon Jones
05-19-2003, 12:42 AM
Hi,

Can someone tell me how I can modify the javascript below (a random link generator) to make it target a specific frame (called "middle")?

Thanks!


<P><!--NOEDIT--><p align="center"><!--webbot bot="HTMLMarkup" startspan --><script>
function random_all(){
var myrandom=Math.round(Math.random()*(document.links.length-1))
window.location=document.links[myrandom].href
}
</script>
<center><form>
<input type="button" value="random link!" onClick="random_all()">
</form></center><!--/NOEDIT-->

chrismiceli
05-19-2003, 01:21 AM
like this

<P><!--NOEDIT--><p align="center"><!--webbot bot="HTMLMarkup" startspan --><script>
function random_all(){
var myrandom=Math.round(Math.random()*(document.links.length-1))
window.frames["0"].src=document.links[myrandom].href //the 0 is the frame that will change
}
</script>
<center><form>
<input type="button" value="random link!" onClick="random_all()">
</form></center><!--/NOEDIT-->

I think they said that the document.links property was deprecated. I forgot what the replacement was. Something like

document.a["0"]
can someone tell me the new way.

Remon Jones
05-19-2003, 01:53 AM
Thanks for the quick response -- I'm no doubt doing something wrong, but when I amend the script to use

window.frames["middle"].src=

in place of

window.location=

I get a 404 message opening in a new window, saying something like "couldn't find random()" --

I can get the script to open random files within its own frame, however every time I modify it to target another frame, the script either doesn't work at all or it opens a new frame and gives me a 404 message.

Again, it's probably some typo on my part, but if anyone has ideas as to what I'm continuing to do wrong, please let me know.

chrismiceli
05-19-2003, 02:41 AM
do you want the random link be on the frame you are targe3ting or the random link be on your frame then open in the other frame, the first script I showed should work for the latter, but the first you will need to do this.
:


<P><!--NOEDIT--><p align="center"><!--webbot bot="HTMLMarkup" startspan --><script>
function random_all(){
var myrandom=Math.round(Math.random()*(document.links.length-1))
window.frames["0"].src=window.frames["0"].document.links[myrandom].href //the 0 is the frame that will change
}
</script>
<center><form>
<input type="button" value="random link!" onClick="random_all()">
</form></center><!--/NOEDIT-->

shlagish
05-19-2003, 03:35 AM
A question of my own:

Could you replace : window.frames["middle"]
by : _top.middle
?

Remon Jones
05-19-2003, 12:31 PM
Thanks in advance for your patience. I tried the suggestion -- and various variations -- but no luck. I probably haven't described my situation adequately, however, so I've created a test case online that perhaps you could evaluate:

The test page is at
http://www.quass.com/random.html

It contains the random generator script and two test links that ideally should open in a new window when the generator button is clicked. (Currently nothing happens when the button is clicked on this test page.) This new test case is not identical to the previous case I described in that the target this time is a brand-new window -- but if I can get the test case to work at random.html, I'll be more than satisfied.

Thanks again