Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

Javascript Cookie Script Needs a Tweak

Question posted by: CR1 (Newbie) on December 6th, 2005 02:14 AM
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

Code: ( text )
  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. //
  37. //
  38. // "Internal" function to return the decoded value of a cookie
  39. //
  40. function getCookieVal (offset) {
  41. var endstr = document.cookie.indexOf (";", offset);
  42. if (endstr == -1)
  43. endstr = document.cookie.length;
  44. return unescape(document.cookie.substring(offset, endstr));
  45. }
  46.  
  47. //
  48. // Function to return the value of the cookie specified by "name".
  49. // name - String object containing the cookie name.
  50. // returns - String object containing the cookie value, or null if
  51. // the cookie does not exist.
  52. //
  53. function GetCookie (name) {
  54. var arg = name + "=";
  55. var alen = arg.length;
  56. var clen = document.cookie.length;
  57. var i = 0;
  58. while (i < clen) {
  59. var j = i + alen;
  60. if (document.cookie.substring(i, j) == arg)
  61. return getCookieVal (j);
  62. i = document.cookie.indexOf(" ", i) + 1;
  63. if (i == 0) break;
  64. }
  65. return null;
  66. }
  67.  
  68. //
  69. // Function to create or update a cookie.
  70. // name - String object object containing the cookie name.
  71. // value - String object containing the cookie value. May contain
  72. // any valid string characters.
  73. // [expires] - Date object containing the expiration data of the cookie. If
  74. // omitted or null, expires the cookie at the end of the current session.
  75. // [path] - String object indicating the path for which the cookie is valid.
  76. // If omitted or null, uses the path of the calling document.
  77. // [domain] - String object indicating the domain for which the cookie is
  78. // valid. If omitted or null, uses the domain of the calling document.
  79. // [secure] - Boolean (true/false) value indicating whether cookie transmission
  80. // requires a secure channel (HTTPS).
  81. //
  82. // The first two parameters are required. The others, if supplied, must
  83. // be passed in the order listed above. To omit an unused optional field,
  84. // use null as a place holder. For example, to call SetCookie using name,
  85. // value and path, you would code:
  86. //
  87. // SetCookie ("myCookieName", "myCookieValue", null, "/");
  88. //
  89. // Note that trailing omitted parameters do not require a placeholder.
  90. //
  91. // To set a secure cookie for path "/myPath", that expires after the
  92. // current session, you might code:
  93. //
  94. // SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true);
  95. //
  96. function SetCookie (name, value) {
  97. var argv = SetCookie.arguments;
  98. var argc = SetCookie.arguments.length;
  99. var expires = (argc > 2) ? argv[2] : null;
  100. var path = (argc > 3) ? argv[3] : null;
  101. var domain = (argc > 4) ? argv[4] : null;
  102. var secure = (argc > 5) ? argv[5] : false;
  103. document.cookie = name + "=" + escape (value) +
  104. ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
  105. ((path == null) ? "" : ("; path=" + path)) +
  106. ((domain == null) ? "" : ("; domain=" + domain)) +
  107. ((secure == true) ? "; secure" : "");
  108. }
  109.  
  110. // Function to delete a cookie. (Sets expiration date to current date/time)
  111. // name - String object containing the cookie name
  112. //
  113. function DeleteCookie (name) {
  114. var exp = new Date();
  115. exp.setTime (exp.getTime() - 1); // This cookie is history
  116. var cval = GetCookie (name);
  117. document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
  118. }
  119.  
  120. </script>
  121. </head>
  122.  
  123.  
  124. <FORM NAME="demoForm" onSubmit="
  125. if(demoForm.UserName.value.length != 0) {
  126. var expdate = new Date ();
  127. expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000));
  128. SetCookie('zip', demoForm.UserName.value, expdate);
  129. alert('Your Zip Code is ' + demoForm.UserName.value + '. Please Press The Continue Button To Enter');
  130. return false;
  131. } else {
  132. alert('Nothing Was Entered For Your Zip Code.');
  133. return false;
  134. }">
  135.  
  136.  
  137. <CENTER>
  138. Enter Your Zip Code: <INPUT TYPE="text" NAME="UserName" SIZE=12>
  139.  
  140.  
  141. <INPUT TYPE="submit" VALUE="Submit Your Zip Code">
  142. <INPUT TYPE="button" VALUE="Continue..."
  143. onClick="
  144. if(GetCookie('zip') == null)
  145. alert('You Forgot To Enter Your Zip Code')
  146. else
  147. window.open('http://www.YourDomain.com/index.php', '_top')">
  148.  
  149. </FORM>
  150. </CENTER>
  151.  
  152.  
  153. I read the cookie with...
  154. <SCRIPT>document.write("Your Zip Code is <b>" + GetCookie('zip') + ".");</SCRIPT>
  155.  
  156.  
  157. --------------------------------------------------------------------------
Last edited by KUB365 : December 6th, 2005 at 02:30 AM.
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
acoder's Avatar
acoder
Site Moderator
10,984 Posts
April 7th, 2008
10:01 AM
#2

Re: Javascript Cookie Script Needs a Tweak
Code: ( text )
  1. window.open('http://www.YourDomain.com/index.php?zip=' + encodeURIComponent(GetCookie('zip')), '_top')

Reply
Reply
Not the answer you were looking for? Post your question . . .
184,038 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top Javascript / DHTML / Ajax Forum Contributors