Trying to get dice generator script

by 7 replies
9
Hi,

I'm having real difficulties getting a dice generator onto my website.
Does anyone know a way to do it?

I've looked into the Javascript but so far the code I'm trying isn't working.

Has anyone got ideas that can help me?
#programming #dice #generator #script
  • Hi Tyler,

    Post the code (and any relevant html) and I'll take a look.

    Cheers,
    Michael

    • [2] replies
    • <script language="JavaScript1.1" src="DiceQuote1.js">

      </script>
      <form>
      <INPUT TYPE="button" onClick="history.go(0)" VALUE="Roll Dice">

      </p>

      </form>

      External Link Script:

      // JavaScript Document

      var randomnumber=Math.floor(Math.random()*12)

      //change the quotes if desired. Add/ delete additional quotes as desired.

      quotes[0]='1'

      quotes[1]='2'

      quotes[2]='3'

      quotes[3]='4'

      quotes[4]='5'

      quotes[5]='6'

      quotes[6]='7'

      quotes[7]='8'

      quotes[8]='9'

      quotes[9]='10'

      quotes[10]='11'

      quotes[11]='12'

      var whichquote=Math.floor(Math.random()*(quotes.length ))
      document.write(quotes[whichquote])

      So basically I've got the form buttons to refresh the page hopefully chucking out a random number on the screen each time.
      • [1] reply
    • Thanks, I'll try that now mate
      • [1] reply
  • Here is a bit better way to do it using jQuery.

    Code:
    <!DOCTYPE html>
    <html>
        <head>
            <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        </head>
        <body>
            <button id="roll">Roll</button>
            
            <script type="text/javascript">
                $(document).ready(function(){
                    var quotes = ["quote 1", "quote 2", "quote 3", "quote 4", "quote 5", "..."];
    
                    $("#roll").click(function(){
                        var random = Math.floor(Math.random() * (quotes.length));
                        alert(quotes[random]);
                    });
                });
            </script>
        </body>
    </html>

Next Topics on Trending Feed