PDA

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



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

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

Twey
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";
</script>

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

Mike

Twey
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.