Advertisement
richarduie

cookie.html

Apr 4th, 2013
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. <html>
  2. <head>
  3. <!--
  4. any browser sniffing tool you care to employ will do -
  5. I used Mark Wilton-Jones' sniffer.js:
  6. http://www.howtocreate.co.uk/jslibs/sniffer.js
  7. -->
  8. <script type="text/javascript" src="sniffer.js"></script>
  9. <script type="text/javascript">
  10. function getCookie(key) {
  11. // convenience reference
  12. var c = document.cookie;
  13. // if none, return null - exit immediately
  14. if ( 0 == c.length ) return null;
  15. // implicit else - test for cookie value for key
  16. var cv_start = c.indexOf(key + "=");
  17. // if no value for key, return null - exit immediately
  18. if (-1 == cv_start) return null;
  19. // implicit else - get value from cookie string
  20. var cv = c.substring(cv_start + (key + "=").length);
  21. // find end of value for key
  22. var cv_end = cv.indexOf( ";" );
  23. // if no trailing semi-colon, this is last value, reset end
  24. if ( -1 == cv_end ) cv_end = cv.length;
  25. // chop trailing parts, if any and return unescaped value
  26. return unescape( cv.substring( 0, cv_end ) );
  27. };
  28. function setCookie( key, value, expiredays) {
  29. // if not given, set default number of days until expiration
  30. // to one year in the future
  31. if ('undefined' == typeof expiredays) expiredays = 365.25;
  32. // get a new Date instance
  33. var expires = new Date();
  34. // convert requested (or default) number of days to milliseconds
  35. expires.setTime( expires.getTime() + 1000*24*60*60 * expiredays );
  36. // add new key/value pair to cookies for page
  37. document.cookie = key + '=' + escape(value) +
  38. ((null == expiredays)?'':(';expires=' + expires.toGMTString()));
  39. };
  40.  
  41. function getString( p ) {
  42. while (true) {
  43. var str = prompt( p, '' );
  44. if (0 < str.length) break;
  45. else alert( 'input must not be empty - try again' );
  46. }
  47. return str;
  48. };
  49.  
  50. function storeUserData() {
  51. setCookie( 'name', getString( 'enter user name' ));
  52. setCookie( 'mail', getString( 'enter email account' ));
  53. setCookie( 'browser', yourBro[1]);
  54. };
  55. function showUserData() {
  56. var rpt = 'name: ' + getCookie( 'name' );
  57. rpt += ' - mail: ' + getCookie( 'mail' );
  58. rpt += ' - browser: ' + getCookie( 'browser' );
  59. alert( rpt );
  60. };
  61. </script>
  62. </head>
  63. <body>
  64. <form>
  65. <p>
  66. <input type="button" value="store cookie data"
  67. onclick="storeUserData()" />
  68. <input type="button" value="show cookie data"
  69. onclick="showUserData()" />
  70. </p>
  71. </form>
  72. </body>
  73. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement