473,385 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Javascript Cookie Script Needs a Tweak

1
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.  
Dec 6 '05 #1
1 6666
acoder
16,027 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. window.open('http://www.YourDomain.com/index.php?zip=' + encodeURIComponent(GetCookie('zip')), '_top')
  2.  
Apr 7 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Phil Powell | last post by:
URL: http://valsignalandet.com/cookiegrab.php?name=valIdentifier This page produces a cookie value and sends it back as HTML using PHP and Javascript to obtain it. This URL will be used as the...
3
by: Ken | last post by:
I am new to PHP. But this seems like a terrific language. I have been using JavaScript up to now but it looks like I will be changing to PHP. Seems to have more potential. I am confused about...
13
by: Craig | last post by:
Hey all, Here's the situation: - two websites, one on domain1 and the other on domain2 - domain1 opens a new window which is a javascript app from domain2 - domain1 needs to communicate with...
7
by: Bert | last post by:
I have been reading the post and the FAQ and have been unable to find anything that will help with my problem. First let me say that I am not a web developer, designer and no next to nothing...
5
by: Connie Walsh | last post by:
Hi: I gleaned a javascript off of the web: http://www.hypergurl.com/popup.html that sets a cookie everytime someone visits your site. If it is the first visit in x number of days then a...
3
by: Jane D | last post by:
I have got a bookmarklet for use with Opera which highlights all occurrences of some text in the displayed page. I find it very useful. Sometimes I need to use two or three different colours...
7
by: spariam | last post by:
I know this subject has been well discussed, but I haven't found exactly what I'm looking for in the archives, or if it's possible. I need to call a cgi (actually, mod_perl) script, but not on...
0
by: JT | last post by:
I posted a question earlier about communicating between a javascript function and vb.net. In the end I decided to try using a cookie. I use the following javascript function (from vb.net) to set...
4
by: Bart | last post by:
Hello, I created a php script in which I call php fumcion which looks like this function confirm($vl) { ?> <head>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.