View Full Version : what's wrong with this script?

08-02-2005, 12:27 PM
<title>.:random page:.</title>
<script type="text/javascript">
if (r<0.3)
document.open "page1.html"
elseif (r>0.3,<0.9)
document.open "page2.html"
document.open "page3.html"

hmmmm...it's probably an easy fix, and i'm just an idiot :P--that's normallly how it is...

08-02-2005, 01:26 PM
Oh, dear. Good job I'm patient.
document.open() doesn't work as you seem to expect it to. Instead, it opens a new (blank) document, which you can then write to with document.write() and finalize with document.close(). This way you can use Javascript to write a whole document from scratch. It doesn't allow you to redirect the browser.
You must enclose function arguments in parentheses.
Commas do not belong in conditions. You probably mean to say "and" (&&).
The "elseif" keyword does not exist. You probably mean to say "else if."
A slightly more common mistake is the Math object. It has a capital M.
You can also neglect the braces for single statements.
The finished code looks like this:

<script type="text/javascript">
var r = Math.random();

if (r < 0.3) window.location.href = "page1.html";
else if (r > 0.3 && r < 0.9) window.location.href = "page2.html";
else window.location.href = "page3.html";

08-02-2005, 01:32 PM
var r = Math.random;You might want to call that method. :D


08-02-2005, 01:35 PM
Whoops. Missed that one :o Edited.

Ian, I suggest you read a Javascript tutorial (http://www.w3schools.com/js/default.asp) before striking out on your own.