inherit
Peabrained Codebreaker
107114
0
Mar 11, 2020 7:47:27 GMT -8
Boy_Wonder
6,249
July 2007
natzy24
|
Post by Boy_Wonder on Jul 29, 2010 2:14:24 GMT -8
Okay I know I'm not much of a javascripter but I'm trying ^.^;; and this is probably stupidly advanced stuff but I need it to finish off a code I'm writing for use on my own forum. I have no idea if this is even possible but what I need is for when a user visits one board for the first time a cookie to be set that'll expire in 24 hours from when they visit the page.
The first time they visit the page they should get one set of text If they visit the page a second time within 24 hours then they should get a different message. If you need to see the code so far then I can post it up.
I hope this is the right page, cause really I'm looking for someone to explain to me in baby steps how to handle this part of the coding.
|
|
inherit
100824
0
May 13, 2012 5:37:49 GMT -8
Michael
14,585
March 2007
wrighty
|
Post by Michael on Jul 29, 2010 2:31:47 GMT -8
To set a cookie you simply use: document.cookie = 'cookiename=cookievalue;expires=date'So for you I'd do something like this: var d = new Date(); d.setDate(d.getDate() + 1).toGMTString(); document.cookie = 'visited=1;expires=' + dThat would set a cookie named "visited" to "1", that would expire exactly 24 hours from the second it's added. That should help with the first stages.
|
|
inherit
Peabrained Codebreaker
107114
0
Mar 11, 2020 7:47:27 GMT -8
Boy_Wonder
6,249
July 2007
natzy24
|
Post by Boy_Wonder on Jul 29, 2010 2:38:54 GMT -8
Okay - and from what I understand that sets the cookie up am I right? -trying to learn-
|
|
inherit
100824
0
May 13, 2012 5:37:49 GMT -8
Michael
14,585
March 2007
wrighty
|
Post by Michael on Jul 29, 2010 2:49:17 GMT -8
Yes it does. It'll add a cookie for the user.
|
|
inherit
Peabrained Codebreaker
107114
0
Mar 11, 2020 7:47:27 GMT -8
Boy_Wonder
6,249
July 2007
natzy24
|
Post by Boy_Wonder on Jul 29, 2010 2:51:22 GMT -8
So say I then wanted to use this cookie to tell the code which text to select how would I go about that? I'm guessing through an if statement somehow but beyond that
|
|
Donovan
New Member
Redskins: 0-0
Posts: 19
inherit
156105
0
Oct 17, 2010 10:46:44 GMT -8
Donovan
Redskins: 0-0
19
July 2010
donovanqb5
|
Post by Donovan on Jul 29, 2010 20:34:22 GMT -8
This method seems to work....
|
|
inherit
Peabrained Codebreaker
107114
0
Mar 11, 2020 7:47:27 GMT -8
Boy_Wonder
6,249
July 2007
natzy24
|
Post by Boy_Wonder on Jul 29, 2010 22:06:56 GMT -8
That was... useful ¬.¬ and mature.
I know I'm new to javascript coding etc but c'mon gimme a bit of a break here please and help me out?
<script language="JavaScript"> // Treasure Chest Code // Created by Nats aka Reno of ShinRas Turks // For use ONLY on http://shinraturk.proboards.com // Do NOT steal or else we will hunt you down //Array goes here item=new Array(); item[0]="nothing, sorry today wasn't your day"; item[1]="200 Karma"; item[2]="nothing, sorry today wasn't your day"; item[3]="a Phoenix Down"; item[4]="nothing, sorry today wasn't your day"; item[5]="A Guardhound Pup"; item[6]="nothing, sorry today wasn't your day"; item[7]="nothing, sorry today wasn't your day"; item[8]="1 Restorative items Bundle"; item[9]="nothing, sorry today wasn't your day"; item[10]="3 Phoenix Downs"; item[11]="2000 Karma"; item[12]="nothing, sorry today wasn't your day"; item[13]="nothing, sorry today wasn't your day";
//randomization var now = new Date(); var secs = now.getSeconds(); var raw_random_number = Math.random(secs); var random_number = Math.round(raw_random_number * (item.length)); if (random_number == item.length){random_number = 0} //set item var itemrand = item[random_number]; //Cookie var d = new Date(); d.setDate(d.getDate() + 1).toGMTString(); document.cookie = 'visited=1;expires=' + d //output goes here document.write("Congratulations! You have won " +itemrand+ ". Please Message the main admin to retrieve your award <br>"); document.write("you can contact the main admin by clicking here <a href='/index.cgi?action=pmsend&to=admin&subject=I%20have%20won%20an%20item&message=I%20have%20won%20an%20item%20from%20the%20treasure%20chest%20the%20item%20is%20as%20follows'>here</a>") document.write("<br>Sorry you can only play the treasure chest once every 24 hours, try again some other time"); --> </script>
This is what I've got so far, I'm not even sure if I've got things in the right order
|
|
inherit
King Oligochaete
126470
0
Feb 24, 2021 12:23:15 GMT -8
Wormopolis
Stop rating plugins with 1 star that dont deserve it.
20,002
June 2008
tbserialkillers
Wormo's Mini-Profile
|
Post by Wormopolis on Jul 29, 2010 23:36:59 GMT -8
so from this point, you want somewhere in your code to check the cookies to see if that cookie you created was already there. if it is, then it doesnt post the treasure chest message.
to search for a cookie, you have to look in document.cookie. you can use a match statement or you can try and parse it out with semicolons and run through the array, but I prefer the match statement:
if (document.cookie.match(/visited=1/)) { code to execute if cookie found } else { code to execute if no cookie found }
I would also warn against using something common like "visited" . you never know who else might have used that exact same cookie name for one of their codes. you want it unique so it doesnt conflict. also keep in mind, that if someone deletes their cookies, they will constantly see the treasure chest code execute.
|
|
inherit
Peabrained Codebreaker
107114
0
Mar 11, 2020 7:47:27 GMT -8
Boy_Wonder
6,249
July 2007
natzy24
|
Post by Boy_Wonder on Jul 29, 2010 23:54:14 GMT -8
Thanks worm! I thought it'd be something like that but I wasn't totally sure ^_^
So the finished code should look something like this:
<script language="JavaScript"> // Treasure Chest Code // Created by Nats aka Reno of ShinRas Turks // With Help from Wormopolis and Michael of PBS // For use ONLY on http://shinraturk.proboards.com // Do NOT steal or else we will hunt you down //Array goes here item=new Array(); item[0]="nothing, sorry today wasn't your day"; item[1]="200 Karma"; item[2]="nothing, sorry today wasn't your day"; item[3]="a Phoenix Down"; item[4]="nothing, sorry today wasn't your day"; item[5]="A Guardhound Pup"; item[6]="nothing, sorry today wasn't your day"; item[7]="nothing, sorry today wasn't your day"; item[8]="1 Restorative items Bundle"; item[9]="nothing, sorry today wasn't your day"; item[10]="3 Phoenix Downs"; item[11]="2000 Karma"; item[12]="nothing, sorry today wasn't your day"; item[13]="nothing, sorry today wasn't your day";
//randomization var now = new Date(); var secs = now.getSeconds(); var raw_random_number = Math.random(secs); var random_number = Math.round(raw_random_number * (item.length)); if (random_number == item.length){random_number = 0} //set item var itemrand = item[random_number]; //Cookie var d = new Date(); d.setDate(d.getDate() + 1).toGMTString(); document.cookie = 'treasurec=1;expires=' + d //output goes here if (document.cookie.match(/treasurec=1/)) { document.write("<br>Sorry you can only play the treasure chest once every 24 hours, try again some other time"); } else { document.write("Congratulations! You have won " +itemrand+ ". Please Message the main admin to retrieve your award <br>"); document.write("you can contact the main admin by clicking here <a href='/index.cgi?action=pmsend&to=admin&subject=I%20have%20won%20an%20item&message=I%20have%20won%20an%20item%20from%20the%20treasure%20chest%20the%20item%20is%20as%20follows'>here</a>") }
--> </script>
right?
Cause I've tested it in a simple test html file and google chrome pulls up this error: Uncaught TypeError: Object 1280562631856 has no method 'toGMTString'
How could I fix this?
|
|
inherit
2671
0
May 14, 2013 14:40:03 GMT -8
Peter
🐺
10,615
February 2002
peter3
|
Post by Peter on Jul 30, 2010 1:20:51 GMT -8
That's probably because toGMTString is deprecated, you probably want to use toUTCString.
|
|
inherit
Peabrained Codebreaker
107114
0
Mar 11, 2020 7:47:27 GMT -8
Boy_Wonder
6,249
July 2007
natzy24
|
Post by Boy_Wonder on Jul 30, 2010 1:22:50 GMT -8
Will that have the same effect as the toGMTString?
Edit: Altered that showing the same error
|
|
inherit
100824
0
May 13, 2012 5:37:49 GMT -8
Michael
14,585
March 2007
wrighty
|
Post by Michael on Jul 30, 2010 1:35:28 GMT -8
Change it to: var d = new Date(); d.setDate(d.getDate() + 1); document.cookie = 'treasurec=1;expires=' + d.toGMTString()
|
|
inherit
Peabrained Codebreaker
107114
0
Mar 11, 2020 7:47:27 GMT -8
Boy_Wonder
6,249
July 2007
natzy24
|
Post by Boy_Wonder on Jul 30, 2010 1:39:54 GMT -8
all right now its showing the you have won something message, but when I then refresh the page its not showing the other message <script language="JavaScript"> // Treasure Chest Code // Created by Nats aka Reno of ShinRas Turks // With Help from Wormopolis and Michael of PBS // For use ONLY on [url]http://shinraturk.proboards.com[/url] // Do NOT steal or else we will hunt you down
//Array goes here item=new Array(); item[0]="nothing, sorry today wasn't your day"; item[1]="200 Karma"; item[2]="nothing, sorry today wasn't your day"; item[3]="a Phoenix Down"; item[4]="nothing, sorry today wasn't your day"; item[5]="A Guardhound Pup"; item[6]="nothing, sorry today wasn't your day"; item[7]="nothing, sorry today wasn't your day"; item[8]="1 Restorative items Bundle"; item[9]="nothing, sorry today wasn't your day"; item[10]="3 Phoenix Downs"; item[11]="2000 Karma"; item[12]="nothing, sorry today wasn't your day"; item[13]="nothing, sorry today wasn't your day";
//randomization var now = new Date(); var secs = now.getSeconds(); var raw_random_number = Math.random(secs); var random_number = Math.round(raw_random_number * (item.length));
if (random_number == item.length){random_number = 0} //set item var itemrand = item[random_number];
//Cookie
var d = new Date(); d.setDate(d.getDate() + 1); document.cookie = 'treasurec=1;expires=' + d.toGMTString()
//output goes here
if (document.cookie.match(/treasurec=1/)) { document.write("<br>Sorry you can only play the treasure chest once every 24 hours, try again some other time"); } else { document.write("Congratulations! You have won " +itemrand+ ". Please Message the main admin to retrieve your award <br>"); document.write("you can contact the main admin by clicking here<a href='/index.cgi?action=pmsend&to=admin&subject=I%20have%20won%20an%20item&message=I%20have%20won%20an%20item%20from%20the%20treasure%20chest%20the%20item%20is%20as%20follows'>here</a>") }
--> </script> is there any place I've messed up anything that you people can see? Edit: Showed a mate it online and she's getting nothing at all and she's using IE testing23456.webs.com/test4.htm < link for its online test
|
|
inherit
100824
0
May 13, 2012 5:37:49 GMT -8
Michael
14,585
March 2007
wrighty
|
Post by Michael on Jul 30, 2010 2:45:53 GMT -8
<script type="text/javascript"> // Treasure Chest Code // Created by Nats aka Reno of ShinRas Turks // With Help from Wormopolis and Michael of PBS // For use ONLY on [url]http://shinraturk.proboards.com[/url] // Do NOT steal or else we will hunt you down
//Array goes here item=new Array(); item[0]="nothing, sorry today wasn't your day"; item[1]="200 Karma"; item[2]="nothing, sorry today wasn't your day"; item[3]="a Phoenix Down"; item[4]="nothing, sorry today wasn't your day"; item[5]="A Guardhound Pup"; item[6]="nothing, sorry today wasn't your day"; item[7]="nothing, sorry today wasn't your day"; item[8]="1 Restorative items Bundle"; item[9]="nothing, sorry today wasn't your day"; item[10]="3 Phoenix Downs"; item[11]="2000 Karma"; item[12]="nothing, sorry today wasn't your day"; item[13]="nothing, sorry today wasn't your day";
//randomization var now = new Date(); var secs = now.getSeconds(); var raw_random_number = Math.random(secs); var random_number = Math.round(raw_random_number * (item.length));
if (random_number == item.length){random_number = 0}
//set item var itemrand = item[random_number];
//output goes here
if (document.cookie.match(/treasurec=1/)) { document.write("<br>Sorry you can only play the treasure chest once every 24 hours, try again some other time"); }else { document.write("Congratulations! You have won " +itemrand+ ". Please Message the main admin to retrieve your award <br>"); document.write("you can contact the main admin by clicking here<a href='/index.cgi?action=pmsend&to=admin&subject=I%20have%20won%20an%20item&message=I%20have%20won%20an%20item%20from%20t he%20treasure%20chest%20the%20item%20is%20as%20follows'>here</a>") ; }
//Cookie
now.setDate(now.getDate() + 1); document.cookie = 'treasurec=1;expires=' + now.toGMTString(); --> </script> Try that? It works for me - test.wserver.co.uk/cookietest.html ------ You were setting the cookie before you checked for it, so no matter what, there was a cookie there!
|
|
inherit
Peabrained Codebreaker
107114
0
Mar 11, 2020 7:47:27 GMT -8
Boy_Wonder
6,249
July 2007
natzy24
|
Post by Boy_Wonder on Jul 30, 2010 3:19:45 GMT -8
xD thanks ^_^!
|
|