Re: javascript, random quotes/images
by "Mike Taylor" <lonewolf(at)one.net>
|
| Date: |
Fri, 15 Sep 2000 00:08:25 -0400 |
| To: |
<hwg-techniques(at)mail.hwg.org> |
| References: |
jps |
| |
todo: View
Thread,
Original
|
|
It looks like you're randomizing two separate arrays and thus they will
never be synched up (well, if you do it enough times, just by the law of
averages you'll eventually get at least one or two times when they'll appear
to have synched). I think if I had to do this, at the quotes segment in the
body of your document, instead of randomizing a second time, I'd set up the
quotes.length value to be equal to pictures.length and then write that out.
Mike
----- Original Message -----
From: "Roger Harness" <magic32(at)jps.net>
To: <hwg-techniques(at)mail.hwg.org>
Sent: Thursday, September 14, 2000 6:35 PM
Subject: javascript, random quotes/images
> dear techniques gods/goddesses,
>
> I'm trying to do a random image/quote page using JavaScript.
>
> I CAN get the page to display a random image, and a random quote, but I'm
> having troubles getting the page to show a random image along WITH the
> coinsiding random quote. (a little image of Mulder appears next to his
> "trust no one" quote, an image of springsteen along with a line of his
from
> an album etc) The funny thing is, SOMEtimes it will work correctly, but if
> you hit refresh/reload enough times, eventually it starts to get mixed up.
> Or sometimes not at all. Or sometimes almost everytime.
>
> I'm still basically a jscript beginner, in a beginning scripting class,
> but/so i think my problem is not so much the JavaScript itself, but more
in
> the way i'm utilizing it along with my html code. (so far my biggest
> problem). And if you notice, i have the two document.write statements
> separated into table cells. This is part of the assignment, not my choice.
>
> So maybe my question is more along the lines of: how can i place my image
> in a cell, and my quote in a cell, and have them coinside? I think I saw
> somewhere where you can turn both the image, and the quote into a varable,
> THEN place 'em into a page, but it seems like there's another, easier (?)
> way. Maybe not?
>
> Anyways, if anyone has the time, and energy, here's the code, and a temp.
> url.
>
> btw, its not a life or death situation, I can ask the teacher next
tuesday,
> I was just trying to figure this one out myself...sorta :)
>
>
>
> <html>
> <head>
> <title>Randomly Generated Quote</title>
>
> <script language="JavaScript">
> <!--hide from older browsers
> // create the array and insert the quotes. Note: quotes should be on one
> line
> var quotes = new Array();
> quotes[0] = "Trust no one.<br>--<i>Fox Mulder</i>";
> quotes[1] = "When you believe it, you'll see it.<br>--<i>L. Bryant</i>";
> quotes[2] = "...blind faith in your leaders, or in anyone will get you
> killed.<br>--<i>Bruce Springsteen</i>";
> quotes[3] = "It's showtime, baby!<br>--<i>L.A. Laker guard, Magic
> Johnson</i>";
> quotes[4] = "Hey Bud, let's party!<br>--<i>Jeff Spicoli</i>";
> quotes[5] = "You will miss 100% of the shots you never
> take<br>--<i>Anonymous</i>";
> quotes[6] = "Did you feel the rush?<br>--<i>Phil Smith</i>";
>
> // create the array for the images
> var pictures = new Array();
> pictures[0] = "<img src='images/mulder.jpg'>";
> pictures[1] = "<img src='images/leslie.jpg'>";
> pictures[2] = "<img src='images/bruce.jpg'>";
> pictures[3] = "<img src='images/magic.jpg'>";
> pictures[4] = "<img src='images/spicoli.jpg'>";
> pictures[5] = "<img src='images/hoop.jpg'>";
> pictures[6] = "<img src='images/rush.jpg'>";
>
>
> // define the random number generator function
> function rand(n) {
> var now = new Date();
> var seed = now.getTime() % 0xffffffff;
> seed = (0x015a4e35 * seed) % 0x7fffffff;
> return (seed >> 16) % n;
> }
>
> //-->
> </script>
> </head>
>
> <body bgcolor="white">
> <h1><font face="arial, helvtica">Moment of Zen</font></h1>
> <p>A random quote brought to you for this exact moment:</p>
>
>
> <table border=0>
> <tr>
> <td>
> <script language="JavaScript">
> <!--hide from older browsers
> //display random image
> document.write(pictures[rand(pictures.length)]);
> //-->
> </script>
> </td>
>
> <td>
> <script language="JavaScript">
> <!--hide from older browsers
>
> //display randome quote
> document.write(quotes[rand(quotes.length)]);
> //-->
> </script></td>
> </tr>
> </table>
>
> </body>
> </html>
>
> http://www.2webslingers.com/js_problem/random-hack4.html
>
> Thanks to anyone who might take a look.
> Sincerely,
>
> Roger Harness
>
>
HWG hwg-techniques mailing list archives,
maintained by Webmasters @ IWA
This page is part of a preserved archive of archives.hwg.org. The site is no longer active and its content is not maintained. For enquiries about this archive, write to archive(at)iwanet.org.