Connecting Tech Pros Worldwide Help | Site Map

Javascript Cookie Script Needs a Tweak

 
LinkBack Thread Tools Search this Thread
  #1  
Old December 6th, 2005, 02:14 AM
CR1 CR1 is offline
Newbie
 
Join Date: Dec 2005
Posts: 1
Default Javascript Cookie Script Needs a Tweak

I found a great cookie script below, but don't know how to make it also pass the values sent to the cookie, to a querystring as well for tracking purposes. Can anyone help? If there was a way to simply pass the values in a cookie to the querystring that would be even easier, but from what I've been able to tell, cookie values can't be passed to a querystring.

I'm sure the answer will help alot of others who are using this script, and would like to see it pass values to a querystring as well.

Thanks Everyone,
Carlton

Expand|Select|Wrap|Line Numbers
  1.  -------------------------------------------------------------------------- 
  2.  
  3. <head>
  4. <script>
  5. //
  6. // Cookie Functions - Second Helping (21-Jan-96)
  7. // Written by: Bill Dortch, hIdaho Design <bdortch@netw.com>
  8. // The following functions are released to the public domain.
  9. //
  10. // The Second Helping version of the cookie functions dispenses with
  11. // my encode and decode functions, in favor of JavaScript's new built-in
  12. // escape and unescape functions, which do more complete encoding, and
  13. // which are probably much faster.
  14. //
  15. // The new version also extends the SetCookie function, though in
  16. // a backward-compatible manner, so if you used the First Helping of
  17. // cookie functions as they were written, you will not need to change any
  18. // code, unless you want to take advantage of the new capabilities.
  19. //
  20. // The following changes were made to SetCookie:
  21. //
  22. // 1. The expires parameter is now optional - that is, you can omit
  23. // it instead of passing it null to expire the cookie at the end
  24. // of the current session.
  25. //
  26. // 2. An optional path parameter has been added.
  27. //
  28. // 3. An optional domain parameter has been added.
  29. //
  30. // 4. An optional secure parameter has been added.
  31. //
  32. // For information on the significance of these parameters, and
  33. // and on cookies in general, please refer to the official cookie
  34. // spec, at:
  35. //
  36. // http://www.netscape.com/newsref/std/cookie_spec.html 
  37. //
  38. //
  39. // "Internal" function to return the decoded value of a cookie
  40. //
  41. function getCookieVal (offset) {
  42. var endstr = document.cookie.indexOf (";", offset);
  43. if (endstr == -1)
  44. endstr = document.cookie.length;
  45. return unescape(document.cookie.substring(offset, endstr));
  46. }
  47.  
  48. //
  49. // Function to return the value of the cookie specified by "name".
  50. // name - String object containing the cookie name.
  51. // returns - String object containing the cookie value, or null if
  52. // the cookie does not exist.
  53. //
  54. function GetCookie (name) {
  55. var arg = name + "=";
  56. var alen = arg.length;
  57. var clen = document.cookie.length;
  58. var i = 0;
  59. while (i < clen) {
  60. var j = i + alen;
  61. if (document.cookie.substring(i, j) == arg)
  62. return getCookieVal (j);
  63. i = document.cookie.indexOf(" ", i) + 1;
  64. if (i == 0) break; 
  65. }
  66. return null;
  67. }
  68.  
  69. //
  70. // Function to create or update a cookie.
  71. // name - String object object containing the cookie name.
  72. // value - String object containing the cookie value. May contain
  73. // any valid string characters.
  74. // [expires] - Date object containing the expiration data of the cookie. If
  75. // omitted or null, expires the cookie at the end of the current session.
  76. // [path] - String object indicating the path for which the cookie is valid.
  77. // If omitted or null, uses the path of the calling document.
  78. // [domain] - String object indicating the domain for which the cookie is
  79. // valid. If omitted or null, uses the domain of the calling document.
  80. // [secure] - Boolean (true/false) value indicating whether cookie transmission
  81. // requires a secure channel (HTTPS). 
  82. //
  83. // The first two parameters are required. The others, if supplied, must
  84. // be passed in the order listed above. To omit an unused optional field,
  85. // use null as a place holder. For example, to call SetCookie using name,
  86. // value and path, you would code:
  87. //
  88. // SetCookie ("myCookieName", "myCookieValue", null, "/");
  89. //
  90. // Note that trailing omitted parameters do not require a placeholder.
  91. //
  92. // To set a secure cookie for path "/myPath", that expires after the
  93. // current session, you might code:
  94. //
  95. // SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true);
  96. //
  97. function SetCookie (name, value) {
  98. var argv = SetCookie.arguments;
  99. var argc = SetCookie.arguments.length;
  100. var expires = (argc > 2) ? argv[2] : null;
  101. var path = (argc > 3) ? argv[3] : null;
  102. var domain = (argc > 4) ? argv[4] : null;
  103. var secure = (argc > 5) ? argv[5] : false;
  104. document.cookie = name + "=" + escape (value) +
  105. ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
  106. ((path == null) ? "" : ("; path=" + path)) +
  107. ((domain == null) ? "" : ("; domain=" + domain)) +
  108. ((secure == true) ? "; secure" : "");
  109. }
  110.  
  111. // Function to delete a cookie. (Sets expiration date to current date/time)
  112. // name - String object containing the cookie name
  113. //
  114. function DeleteCookie (name) {
  115. var exp = new Date();
  116. exp.setTime (exp.getTime() - 1); // This cookie is history
  117. var cval = GetCookie (name);
  118. document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
  119. }
  120.  
  121. </script>
  122. </head>
  123.  
  124.  
  125. <FORM NAME="demoForm" onSubmit="
  126. if(demoForm.UserName.value.length != 0) {
  127. var expdate = new Date (); 
  128. expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000)); 
  129. SetCookie('zip', demoForm.UserName.value, expdate); 
  130. alert('Your Zip Code is ' + demoForm.UserName.value + '. Please Press The Continue Button To Enter');
  131. return false;
  132. } else {
  133. alert('Nothing Was Entered For Your Zip Code.');
  134. return false;
  135. }">
  136.  
  137.  
  138. <CENTER>
  139. Enter Your Zip Code: <INPUT TYPE="text" NAME="UserName" SIZE=12>
  140.  
  141.  
  142. <INPUT TYPE="submit" VALUE="Submit Your Zip Code">
  143. <INPUT TYPE="button" VALUE="Continue..."
  144. onClick="
  145. if(GetCookie('zip') == null)
  146. alert('You Forgot To Enter Your Zip Code')
  147. else 
  148. window.open('http://www.YourDomain.com/index.php', '_top')">
  149.  
  150. </FORM>
  151. </CENTER>
  152.  
  153.  
  154. I read the cookie with... 
  155. <SCRIPT>document.write("Your Zip Code is <b>" + GetCookie('zip') + ".");</SCRIPT>
  156.  
  157.  
  158. --------------------------------------------------------------------------
  159.  

Last edited by KUB365; December 6th, 2005 at 02:30 AM.
Reply
  #2  
Old April 7th, 2008, 10:01 AM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,228
Default

Expand|Select|Wrap|Line Numbers
  1. window.open('http://www.YourDomain.com/index.php?zip=' + encodeURIComponent(GetCookie('zip')), '_top')
  2.  
Reply
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.