Задача: qForms, библиотека типичного функционала валидации/построения/связки html-форм
Исходник: cookies.js :: qForms api-139, язык: javascript [code #153, hits: 11634]
автор: - [добавлен: 28.05.2006]
  1. // initialize workspace variables
  2. var _c_dToday = new Date();
  3. var _c_iExpiresIn = 90;
  4. var _c_strName = self.location.pathname;
  5.  
  6. /******************************************************************************
  7. Required Functions
  8. ******************************************************************************/
  9. // retrieve a cookie from the browser
  10. function _getCookie(name){
  11. var iStart = document.cookie.indexOf(name + "=");
  12. var iLength = iStart + name.length + 1;
  13. if( (iStart == -1) || (!iStart && (name == document.cookie.substring(0)) ) ) return null;
  14. var iEnd = document.cookie.indexOf(";", iLength);
  15. if( iEnd == -1 ) iEnd = document.cookie.length;
  16. return unescape(document.cookie.substring(iLength, iEnd));
  17. }
  18.  
  19. // set a cookie to the browser
  20. function _setCookie(name, value, expires, path, domain, secure){
  21. document.cookie = name + "=" + escape(value) +
  22. ( (expires) ? ";expires=" + expires.toGMTString() : "") +
  23. ( (path) ? ";path=" + path : "") +
  24. ( (domain) ? ";domain=" + domain : "") +
  25. ( (secure) ? ";secure" : "");
  26. }
  27.  
  28. function _deleteCookie(name, path, domain){
  29. if (Get_Cookie(name)) document.cookie = name + "=" +
  30. ( (path) ? ";path=" + path : "") +
  31. ( (domain) ? ";domain=" + domain : "") +
  32. ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
  33. }
  34.  
  35. function _createCookiePackage(struct){
  36. var cookie = "";
  37. for( key in struct ){
  38. if( cookie.length > 0 ) cookie += "&";
  39. cookie += key + ":" + escape(struct[key]);
  40. }
  41. return cookie;
  42. }
  43.  
  44. function _readCookiePackage(pkg){
  45. struct = new Object();
  46. // break the package into key/value pairs
  47. var a = pkg.split("&");
  48. // loop through the array and seperate the key/value pairs
  49. for( var i=0; i < a.length; i++ ) a[i] = a[i].split(":");
  50. // convert the values into a structure
  51. for( var i=0; i < a.length; i++ ) struct[a[i][0]] = unescape(a[i][1]);
  52. // return the structure
  53. return struct;
  54. }
  55.  
  56. /******************************************************************************
  57. qForm Methods
  58. ******************************************************************************/
  59. // define qForm loadFields(); prototype
  60. function _qForm_loadFields(){
  61. var strPackage = _getCookie("qForm_" + this._name + "_" + _c_strName);
  62. // there is no form saved
  63. if( strPackage == null ) return false;
  64.  
  65. this.setFields(_readCookiePackage(strPackage), null, true);
  66. }
  67. qForm.prototype.loadFields = _qForm_loadFields;
  68.  
  69. // define qForm saveFields(); prototype
  70. function _qForm_saveFields(){
  71. var expires = new Date(_c_dToday.getTime() + (_c_iExpiresIn * 86400000));
  72. var strPackage = _createCookiePackage(this.getFields());
  73. _setCookie("qForm_" + this._name + "_" + _c_strName, strPackage, expires);
  74. }
  75. qForm.prototype.saveFields = _qForm_saveFields;
  76.  
  77. // define qForm saveOnSubmit(); prototype
  78. function _qForm_saveOnSubmit(){
  79. // grab the current onSubmit() method and append the saveFields() method to it
  80. var fn = _functionToString(this.onSubmit, "this.saveFields();");
  81. this.onSubmit = new Function(fn);
  82. }
  83. qForm.prototype.saveOnSubmit = _qForm_saveOnSubmit;
  84.  
/******************************************************************************
qForm JSAPI: Cookie Library

Author: Dan G. Switzer, II
Build: 105
******************************************************************************/
Тестировалось на: IE 6.0 SP2, Mozilla FF 1.5, Opera 8.5

+добавить реализацию