Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Aug 2002
    Location
    New York
    Posts
    152
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Question Random facts help

    yeah, i searched this forum for the answer, but i didnt find it, btw...

    i want to have a list of facts (i have about 25 of them) randomly display one at a time per page view. that's pretty self-explanitory enough.

    any questions? i'll gladly answer

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    <html>
    <body>
    <script language="javascript">
    var arrFacts = new Array("fact1","fact2","fact3");
    var index = Math.floor(Math.random()*(arrFacts.length-1));
    document.write(arrFacts[index]);
    document.close()
    </script>
    </body>
    </html>

  • #3
    Regular Coder
    Join Date
    Aug 2002
    Location
    New York
    Posts
    152
    Thanks
    12
    Thanked 0 Times in 0 Posts
    thanks. :tips hat:

    for some facts, i need to use quotes ("), how can i use these without making it think that i'm ending the fact?

  • #4
    New Coder
    Join Date
    Jun 2002
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts

    okay, I'll do it...

    Takes care of the quotation marks...

    <html>
    <body>
    <script language="javascript">
    var arrFacts = new Array("bob said, 'hi'.","'hello',said bob","bob's just \"weird\"");
    var index = Math.floor(Math.random()*(arrFacts.length-1));
    document.write(arrFacts[index]);
    document.close()
    </script>
    </body>
    </html>

    I think those work. First two quotes I just used the other kind of quote. The third quote, I used escape characters. I also used a comma there to add affect

    Good Luck
    If at first you don't succeed, spend more time online researching javascript!
    Beck

  • #5
    Regular Coder
    Join Date
    Aug 2002
    Location
    New York
    Posts
    152
    Thanks
    12
    Thanked 0 Times in 0 Posts
    okay, thanks. and do i need the forward slash when using other characters like dashes and the percent sign?

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    no, only with " or ', depending on your string delimeter used.
    if you use ' as your string delimeter and you have ' within the string, you need to put \' inside the string.
    if you use " as your string delimeter and you have " within the string, you need to put \" inside the string.

    "bob's just \"weird\""

    is the same as:

    'bob\'s just "weird"'


  •  

    Posting Permissions

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