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 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2002
    Location
    Seattle, WA USA
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Alert box problem: Javascript inside?

    Hi, this is probably a silly question, but I am trying to do a "joke" page for a friend, and what I want to do, is have a button that says "Don't Click me" and of course, my friend will go ahead and click it, and then an alert box will appear saying "I told you so" then when you press OK, it says something else like "You asked for it" and every time you press ok, it will tell you something new... I know I could just go like
    OnClick="alert('I told you so');alert('You asked for it')" etc etc,
    but what I want to do is have the text from the alert be completly random. From an external .js file... So I suck with all this javascript stuff, and thats why I came here... this is what I have so far:
    Code:
    <html>
    
    	<head>
    		<title> My Silly Page! </title>
    
    <link rel="stylesheet" href="files/style.css">
    <script src="files/random.js">
    </script>
    
    	</head>
    
    <body>
    
    <button onClick="alert('random_content()')">Do <u>NOT</u> click me!</button>
    
    ...
    ...
    ...
    Then in the random.js file, I have:

    Code:
    <!--
    
    /*
    Random Content Script- By JavaScript Kit(http://www.javascriptkit.com) 
    Over 200+ free JavaScripts here!
    */
    
    function random_content(){
    var mycontent=new Array()
    //specify random content below.
    mycontent[1]='I told you so'
    mycontent[2]='You asked for it'
    mycontent[3]='too bad'
    mycontent[4]='Now what?'
    mycontent[5]='etc etc etc'
    
    
    var ry=Math.floor(Math.random()*mycontent.length)
    if (ry==0)
    ry=1
    document.write(mycontent[ry])
    }
    random_content()
    //-->
    so basically that is what I want to do... and at the end of the js file, it says random_content() again, so that it will repeat alerting if possible or something...

    That is the main idea, but I need to know how to do it... since, putting alert('random_content()') did not work (it appeared as text, not as the actual random content)

    Any help is greatly appreciated!
    Thanks!

  • #2
    New Coder
    Join Date
    Jul 2002
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This should do the trick

    I have included the script in the page to show you that it works


    <html>
    <head>
    <title> My Silly Page! </title>
    </head>
    <body>
    <button onClick="bug_me()">Do <u>NOT</u> click me!</button>



    <script>
    <!--
    say_what=new Array()
    say_what[say_what.length]='I told you so'
    say_what[say_what.length]='You asked for it'
    say_what[say_what.length]='too bad'
    say_what[say_what.length]='Now what?'
    say_what[say_what.length]='etc etc etc'

    function bug_me(){
    rdm=Math.round(Math.random()*say_what.length-1)
    alert(say_what[rdm])
    }
    //-->
    </script>
    </body>
    </html>

  • #3
    New Coder
    Join Date
    Jun 2002
    Location
    Seattle, WA USA
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thank you!

    It worked like a charm =)


    Thanks a lot!


  •  

    Posting Permissions

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