Quick Help for Cookie script?

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Oct 2005
Posts: 240
Reputation: Inny is an unknown quantity at this point 
Solved Threads: 6
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

Quick Help for Cookie script?

 
0
  #1
Jan 20th, 2007
hello again Dani web folk, Im needing a little help with an example script. This cookie setter is prewritten but I want to set it up for use on my site but im not sure how. I want to redirect from here

http://herproom.5.forumer.com/index.php?

to here

http://download3-1.files-upload.com/.../20/22/RP.html

only once, the first time someone visits my site.

Please help me configure this code? much appreciated.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script type="text/javascript">
  2. // adapted from http://evolt.org/article/list/20/416/
  3. // <![CDATA[
  4. var Splash = GetCookie('SplashSkip');
  5. ReDirect('/welcome');
  6. function ReDirect (URL) {
  7. SetCookie('SplashSkip','TRUE',1);
  8. if (Splash == 'TRUE') {
  9. window.location=(URL);
  10. }
  11. }
  12. function getCookieVal (offset) {
  13. var endstr = document.cookie.indexOf (";", offset);
  14. if (endstr == -1)
  15. endstr = document.cookie.length;
  16. return unescape(document.cookie.substring(offset, endstr));
  17. }
  18. function GetCookie (name) {
  19. var arg = name + "=";
  20. var alen = arg.length;
  21. var clen = document.cookie.length;
  22. var i = 0;
  23. while (i < clen) {
  24. var j = i + alen;
  25. if (document.cookie.substring(i, j) == arg)
  26. return getCookieVal (j);
  27. i = document.cookie.indexOf(" ", i) + 1;
  28. if (i == 0) break;
  29. }
  30. return null;
  31. }
  32. function SetCookie(name, value, expDays, path, domain, secure) {
  33. // Set cookie with name, value etc provided
  34. // in function call and date from above
  35. // Number of days the cookie should persist NB expDays='' or undef. => non-persistent
  36. if (expDays != null ) {
  37. var expires = new Date();
  38. expires.setTime(expires.getTime() + (expDays*24*60*60*1000));
  39. }
  40. var curCookie = name + "=" + escape(value) +
  41. ((expires) ? "; expires=" + expires.toGMTString() : "") +
  42. ((path) ? "; path=" + path : "") +
  43. ((domain) ? "; domain=" + domain : "") +
  44. ((secure) ? "; secure" : "");
  45. document.cookie = curCookie;
  46. }
  47. // ]]>
  48. </script>
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: Quick Help for Cookie script?

 
0
  #2
Jan 21st, 2007
Providing the cookie scipt you're using works correctly:

Change:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var Splash = GetCookie('SplashSkip');
  2. ReDirect('/welcome');

To:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var Splash = GetCookie('SplashSkip');
  2. if(Splash == null){
  3. ReDirect('/welcome');
  4. }

Remember to clear your cookies to test (I find Opera is best for testing cookies)
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 240
Reputation: Inny is an unknown quantity at this point 
Solved Threads: 6
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

Re: Quick Help for Cookie script?

 
0
  #3
Jan 21st, 2007
Thanks, I dont think I put the urls in right place though, or something, it didnt redirect at all? was this right or did I forget something? index of?

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script type="text/javascript"> // adapted from http://evolt.org/article/list/20/416/ // <![CDATA[ var Splash = GetCookie('SplashSkip'); if(Splash == null){ ReDirect('/welcome'); }
  2. function ReDirect (http://herproom.5.forumer.com/index.php?) { SetCookie('SplashSkip','TRUE',1); if (Splash == 'TRUE') { window.location=(http://download3-1.files-upload.com/.../20/22/RP.html
  3. ); } } function getCookieVal (offset) { var endstr = document.cookie.indexOf (";", offset); if (endstr == -1) endstr = document.cookie.length; return unescape(document.cookie.substring(offset, endstr)); } function GetCookie (name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while (i < clen) { var j = i + alen; if (document.cookie.substring(i, j) == arg) return getCookieVal (j); i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break; } return null; } function SetCookie(name, value, expDays, path, domain, secure) { // Set cookie with name, value etc provided // in function call and date from above // Number of days the cookie should persist NB expDays='' or undef. => non-persistent if (expDays != null ) { var expires = new Date(); expires.setTime(expires.getTime() + (expDays*24*60*60*1000)); } var curCookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : ""); document.cookie = curCookie; } // ]]> </script>






Originally Posted by MattEvans View Post
Providing the cookie scipt you're using works correctly:

Change:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var Splash = GetCookie('SplashSkip');
  2. ReDirect('/welcome');

To:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var Splash = GetCookie('SplashSkip');
  2. if(Splash == null){
  3. ReDirect('/welcome');
  4. }

Remember to clear your cookies to test (I find Opera is best for testing cookies)
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: Quick Help for Cookie script?

 
0
  #4
Jan 21st, 2007
Sorry, I missed a bit of code you wrote in the original post:

You do this test in the ReDirect function:

if(Splash == "TRUE"){ };

that test should be a test for Splash NOT being "TRUE". Otherwise; your redirect will only happen when the user has visited the page at least once.

The cookie code your using should return a null if the cookie isn't found; but perhaps it's better to test for it not being "TRUE" just in case.

So, the top part of your code should read:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var Splash = GetCookie('SplashSkip');
  2. ReDirect('/welcome');
  3. function ReDirect (URL) {
  4. if (Splash != 'TRUE') {
  5. SetCookie('SplashSkip','TRUE',1);
  6. window.location=(URL);
  7. }
  8. }
Last edited by MattEvans; Jan 21st, 2007 at 2:23 pm.
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 240
Reputation: Inny is an unknown quantity at this point 
Solved Threads: 6
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

Re: Quick Help for Cookie script?

 
0
  #5
Jan 21st, 2007
is this correct then? Ive entered the url to direct to in the right part?

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script type="text/javascript"> // adapted from http://evolt.org/article/list/20/416/ // <![CDATA[var Splash = GetCookie('SplashSkip'); ReDirect('/welcome'); function ReDirect (http://download3-1.files-upload.com/2007-01/20/22/RP.html) { if (Splash != 'TRUE') { SetCookie('SplashSkip','TRUE',1); window.location=(URL); } } function getCookieVal (offset) { var endstr = document.cookie.indexOf (";", offset); if (endstr == -1) endstr = document.cookie.length; return unescape(document.cookie.substring(offset, endstr)); } function GetCookie (name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while (i < clen) { var j = i + alen; if (document.cookie.substring(i, j) == arg) return getCookieVal (j); i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break; } return null; } function SetCookie(name, value, expDays, path, domain, secure) { // Set cookie with name, value etc provided // in function call and date from above // Number of days the cookie should persist NB expDays='' or undef. => non-persistent if (expDays != null ) { var expires = new Date(); expires.setTime(expires.getTime() + (expDays*24*60*60*1000)); } var curCookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : ""); document.cookie = curCookie; } // ]]> </script>
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: Quick Help for Cookie script?

 
0
  #6
Jan 21st, 2007
this line:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function ReDirect (http://download3-1.files-upload.com/2007-01/20/22/RP.html) {

should be:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function ReDirect (URL) {

and if that's the page you want to redirect to; this line:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. ReDirect('/welcome');

should be:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. ReDirect('http://download3-1.files-upload.com/2007-01/20/22/RP.html');
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 240
Reputation: Inny is an unknown quantity at this point 
Solved Threads: 6
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

Re: Quick Help for Cookie script?

 
0
  #7
Jan 21st, 2007
Ahh, Thankyou!

Edit~ ok heres what I have but it still does nothing. forgot to set cookie name, value,path etc etc ???how to do that for persistant cookie

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script type="text/javascript"> // adapted from http://evolt.org/article/list/20/416/ // <![CDATA[var Splash = GetCookie('SplashSkip'); ReDirect('http://download3-1.files-upload.com/2007-01/20/22/RP.html'); function ReDirect (URL) { if (Splash != 'TRUE') { SetCookie('SplashSkip','TRUE',1); window.location=(URL); } } function getCookieVal (offset) { var endstr = document.cookie.indexOf (";", offset); if (endstr == -1) endstr = document.cookie.length; return unescape(document.cookie.substring(offset, endstr)); } function GetCookie (name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while (i < clen) { var j = i + alen; if (document.cookie.substring(i, j) == arg) return getCookieVal (j); i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break; } return null; } function SetCookie(name, value, expDays, path, domain, secure) { // Set cookie with name, value etc provided // in function call and date from above // Number of days the cookie should persist NB expDays='' or undef. => non-persistent if (expDays != null ) { var expires = new Date(); expires.setTime(expires.getTime() + (expDays*24*60*60*1000)); } var curCookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : ""); document.cookie = curCookie; } // ]]> </script>
Last edited by Inny; Jan 21st, 2007 at 11:43 pm. Reason: still not quite right....
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: Quick Help for Cookie script?

 
0
  #8
Jan 22nd, 2007
When you set that cookie, it will default to only be available to the subdomain of the page that set the cookie, and possibly only the folder that set that cookie.

A site-wide cookie should have:

Path = "/"
Domain = "yourdomain.tld" (without the www)

For persistance; set the date 100 years in the future and remember to update it if its about to expire XD

So, if you don't set the path and domain, that cookie might only be available to pages in the folder of the page that set the cookie.

Does the splash one-time only functionality work on the page that set the cookie? It is working ok on my server.

If you are entering code in one continuous line like it's coming up in your posts, it's not going to work unless you remove all the comments... (I'd advise putting the code back into different lines) The first time the javascript engine sees // it ignores all data until it reaches a line break...
Last edited by MattEvans; Jan 22nd, 2007 at 12:44 am.
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 240
Reputation: Inny is an unknown quantity at this point 
Solved Threads: 6
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

Re: Quick Help for Cookie script?

 
0
  #9
Jan 22nd, 2007
Try to save this wordpad to see how its layed out, it just goes one line when cut n pasted here

<a href="http://www.fileden.com/files/2006/9/25/238265/splasher.rtf" title="splasher.rtf"> splasher.rtf </a>

i think this is right now but its still not working, I have grip marks on my mouse!
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: Quick Help for Cookie script?

 
0
  #10
Jan 22nd, 2007
Ok, the main problem there; was with quoting - where you tried to put the cookie domain inside the cookie setting function directly you quoted it so that a load of the script became quoted (quoted parts in <script> tags are generally treated as text, or as an error). That's quite a weird construct (multiple ternary operations concatenated to generate the cookie string). It wasn't very neatly written either.

I've attached a copy of the file that works, and you can see it working at http://www.fusiongroups.net/cookie.html.

You gotta beware of those quotes:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1.  
  2. "this is a "test" is a test" = illegal, and will probably crash the script.
  3. "this 'is a 'test' is a test" = legal, even though there's an odd number of ' quotes
  4. 'this is a "test" is a test' = should be legal aswell.
  5.  
  6. "this is a \"test\" is a test" = legal because the \ character means 'treat the next character as text, even if it should do something special'
  7.  
  8. "this is a test' - illegal, and anything following that, until a " is reached will be treated as text, this will potentially crash the script unless there's an inverted error like this in exactly the right place.
  9.  

check out:

http://www.quirksmode.org/js/strings.html
Last edited by MattEvans; Jan 22nd, 2007 at 9:50 pm.
Attached Files
File Type: txt splasher.txt (1.4 KB, 1 views)
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC