The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 21, 2019, 12:10:52 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: javascript error?  (Read 3139 times)
jammaster82
Lifer
*****
Offline Offline

Posts: 666


Thats craigs list for ya


View Profile
« on: January 31, 2008, 02:38:05 PM »

Code:
<html>


<script type="text/javascript" language="javascript">

function RollDice()
{
var randomnumber = Math.floor(Math.random()*6)+1; /* generates a random number from 0 to 5, if you want to get a number between 0 and 100 just change the 6 to 1001 */
                document.write(randomnumber);
                return true;
}


</script>


<input type="button" value="Roll A Die" onclick="RollDice()" >
</html>




it gives me the random number but then says 'loading..'

if i do an alert() instead of document.write it returns okay...

How would i add lines to the document inside that function
and return, not that i need to do it or anything but always love
to learn.. why does it hang on the document.write?
« Last Edit: January 31, 2008, 03:24:44 PM by jammaster82 » Logged

The watched pot, never boils... But if you walk away from it , the soup burns.  What gives?
DangerMouse
Expert
****
Offline Offline

Posts: 244



View Profile
« Reply #1 on: January 31, 2008, 04:00:31 PM »

I know nowt about javascipt, but I'd imagine its something to do with scope? Variables are normally private to a function, in this instance write is being called on a 'document' object, but that 'document' object is not visible.

I could be totally barking up the wrong tree here, its just a guess.

DM
Logged
manu`
Rookie
**
Offline Offline

Posts: 10



View Profile
« Reply #2 on: January 31, 2008, 04:21:00 PM »

Code:
<html>
<script type="text/javascript" language="javascript">
function RollDice()
{
var randomnumber = Math.floor(Math.random()*7);
if(randomnumber == 0)
{
randomnumer++;
}
document.forms[0].dice.value = randomnumber;
}
</script>


<form name="yourname" method="get">
<input type="button" value="Roll A Die" onclick="RollDice()" >
<input type="text" name="dice" value="" readonly>
</form>
</html>

this should work.
Logged

No links in signatures please
jammaster82
Lifer
*****
Offline Offline

Posts: 666


Thats craigs list for ya


View Profile
« Reply #3 on: January 31, 2008, 07:56:17 PM »

AH,... thank you both...



Code:
function RollDice()
{
var randomnumber = Math.floor(Math.random()*7);
if(randomnumber == 0)
{
randomnumber++;
}
document.forms[0].dice.value = randomnumber;
}

does this not produce 7 different random numbers
at first until modified,returning a 1 for both 0 and 1?

in the above quoted code arent there
eight possible random numbers at first?

--> 0,1,2,3,4,5,6 and 7 ?

« Last Edit: January 31, 2008, 09:42:17 PM by jammaster82 » Logged

The watched pot, never boils... But if you walk away from it , the soup burns.  What gives?
manu`
Rookie
**
Offline Offline

Posts: 10



View Profile
« Reply #4 on: February 01, 2008, 03:30:09 AM »

change randomnumber++ to randomnumber = Math.floor(Math.random()*7);
now it generates a new random number from 1 to 6
Logged

No links in signatures please
jammaster82
Lifer
*****
Offline Offline

Posts: 666


Thats craigs list for ya


View Profile
« Reply #5 on: February 01, 2008, 05:46:12 AM »

what if its zero again?>

« Last Edit: February 01, 2008, 05:56:21 AM by jammaster82 » Logged

The watched pot, never boils... But if you walk away from it , the soup burns.  What gives?
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!