Results 1 to 4 of 4

Thread: what's wrong with this script?

  1. #1
    Join Date
    Jul 2005
    Location
    Kuwait-I'm American
    Posts
    127
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question what's wrong with this script?

    Code:
    <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...
    //\\ //\\// || //\\//\\ //\\ ||_
    SOFTWARE

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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:
    Code:
    <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>
    Last edited by Twey; 08-02-2005 at 01:35 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey
    var r = Math.random;
    You might want to call that method.

    Mike

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Whoops. Missed that one Edited.

    Ian, I suggest you read a Javascript tutorial before striking out on your own.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •