This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Feb 7th, 2013  |  syntax: HTML  |  size: 2.85 KB  |  views: 34  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <html>
  3. <head>
  4.     <title>Form Session</title>
  5. </head>
  6. <body>
  7. <script type="text/javascript">
  8. <!--
  9. /**
  10. * Сохраняем форму. Функция принимает ссылку на форму. Форма должна иметь
  11. * уникальный аттрибут ID.
  12. */
  13. function saveFormSession(form) {
  14.  if(!form||!form.id||!/^[^;=]+$/.test(form.id)) return;
  15.  var data="", tok, el, safe_name;
  16.  for(var i=0; i<form.elements.length; i++) {
  17.    if((el=form.elements[i]).name==""||el.getAttribute("skip_form_save")!=null) continue;
  18.    safe_name=el.name.replace(/([)\\])/g, "\\$1");
  19.    switch(el.type) {
  20.      case "text":
  21.      case "textarea": tok="v("+safe_name+"):"+el.value.replace(/([|\\])/g, "\\$1")+"||"; break;
  22.      case "radio":
  23.      case "checkbox": tok="s("+safe_name+"):"+(el.checked? "1": "0")+"||"; break;
  24.      case "select-one": tok="i("+safe_name+"):"+(el.selectedIndex)+"||"; break;
  25.      default: tok="";
  26.    }
  27.    data+=tok;
  28.  }
  29.  if(data>=4000) return alert("Can't save form into cookie, to much data...");
  30.  document.cookie="ses"+form.id+"="+escape(data);
  31. }
  32. /**
  33. * Восстановить значение формы. Форма должна иметь уникальный атттрибут ID.
  34. */
  35. function restoreFormSession(form) {
  36.   if(!form||!form.id||!/^[^;=]+$/.test(form.id)) return false;
  37.   var strt, end, data, nm, dat;
  38.   if((strt=document.cookie.indexOf("ses"+form.id))<0) return false;
  39.   if((end=document.cookie.indexOf(";", strt + form.id.length + 3))<0) end=document.cookie.length;
  40.   data=unescape(document.cookie.substring(strt + form.id.length + 4, end)).split("||");
  41.   for(var i=0; i<data.length-1; i++) {
  42.      nm=/^[vsi]\(((?:[^)\\]|(?:\\\))|(?:\\\\))+)\)\:/.exec(data[i]);
  43.      nm[1]=nm[1].replace(/\\([)\\])/g, "$1");
  44.      dat=data[i].substr(nm[0].length).replace(/\\([|\\])/g, "$1");
  45.      switch(data[i].charAt(0)) {
  46.        case "v": form.elements[nm[1]].value=dat; break;
  47.        case "s": form.elements[nm[1]].checked=(dat=="1"? true: false); break;
  48.        case "i": form.elements[nm[1]].selectedIndex=dat; break;
  49.      }
  50.   }
  51. }
  52. //-->
  53. </script>
  54. <!-- Пример использования -->
  55. <form id="test1">
  56. <input name="text[0]" type="text" /><br />
  57. <input name="skiped" type="text" skip_form_save="true" /><br />
  58. <input name="pass" type="password" /><br />
  59. <input name="radio" type="radio" /><br />
  60. <input name="check" type="checkbox" /><br />
  61. <textarea name="txt"></textarea><br />
  62. <select name="sel">
  63. <option selected="selected">Test1</option>
  64. <option>Test2</option>
  65. <option>Test3</option>
  66. </select><br />
  67. <input type="button" onclick="saveFormSession(this.form)" value="Save form" />&nbsp;
  68. <input type="button" onclick="restoreFormSession(this.form)" value="Restore form" />
  69. </form>
  70. </body>
  71. </html>
clone this paste RAW Paste Data