Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-31-2011, 04:51 PM   PM User | #1
Undisclosed
New to the CF scene

 
Join Date: Aug 2011
Posts: 7
Thanks: 3
Thanked 0 Times in 0 Posts
Undisclosed is an unknown quantity at this point
Random Number But Only Once

This is quite easy yet hard.

I'm trying to apply something in my forums, where a member could post something let's say:

"Hi, I am number [random]1, 6[/random]"

And what would happen is that, a number from 1 to 6 would be the content, for example, it would be:

"Hi, I am number 3"

But if you refresh the page, it would still be the same number. Like it won't be randomized again.

So how do I make a javascript/html code that would make a one time number randomizer.

Last edited by Undisclosed; 08-31-2011 at 04:57 PM..
Undisclosed is offline   Reply With Quote
Old 08-31-2011, 04:54 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,105
Thanks: 197
Thanked 2,422 Times in 2,400 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
If you refresh the page everything on that page is lost. You must use a cookie to store information to be carried over to another session. Remember that people can (and do) delete cookies in their browser.


All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 08-31-2011, 05:07 PM   PM User | #3
M.Jackson
Regular Coder

 
Join Date: Aug 2011
Posts: 120
Thanks: 1
Thanked 15 Times in 15 Posts
M.Jackson is an unknown quantity at this point
Seems a bit odd, but ok.

You do not want a call to random() to be included in the inline message. You want the output of random() to be posted in the message. To get that, I would (assuming that you have a toolbar in the typing area) put an interface to random() that prompts for parameters then posts it's result in the message.
M.Jackson is offline   Reply With Quote
Old 08-31-2011, 06:40 PM   PM User | #4
Undisclosed
New to the CF scene

 
Join Date: Aug 2011
Posts: 7
Thanks: 3
Thanked 0 Times in 0 Posts
Undisclosed is an unknown quantity at this point
Yeah, every time I refresh the page, the numbers changes.
Is there a way to convert them to plain text, right after I submit.

Right now here's my code:
Code:
<SCRIPT LANGUAGE="JavaScript">

num= new Array(6);
for (i=0; i<1; i++) {
num[i]=Math.floor(6*Math.random()+1);

document.write(num[i]);
}

</SCRIPT>
I would do the cookie thing, but I don't know how to do that. >.<

Last edited by Undisclosed; 08-31-2011 at 06:44 PM..
Undisclosed is offline   Reply With Quote
Old 08-31-2011, 07:28 PM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,105
Thanks: 197
Thanked 2,422 Times in 2,400 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
You cannot do anything after a submit! That wipes out and reloads the page!

Cookies have been covered very many times in this forum. Try using the Search feature.

document.write statements must be run before the page finishes loading. Any document.write statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page. So document.write is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.

To convert numbers to their written text form:-

Code:
<script type = "text/javascript">

var nums = ["One", "Two", "Three", "Four", "Five", "Six"];
var len = nums.length;
var x = Math.floor(len*Math.random());
alert (nums[x]);  // a random number from One to Six

</script>
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 08-31-2011 at 07:31 PM..
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:31 AM.


Advertisement
Log in to turn off these ads.