「戻る」としても,フォームに値を残す
たぶん,どっかにもあるんだろうけど,作ってみました.
ぱくってきたところ(なに
- URI
- http://members.jcom.home.ne.jp/jintrick/Personal/Identifier.html
- Cookie
- http://www.hawk.34sp.com/stdpls/dhtml/samples/cookie_manager.html
<SCRIPT LANGUAGE="JAVASCRIPT"> <!-- // SSLじゃなきゃSSLにする url = location.href.replace(/^http:/, "https:"); if (url != location.href) location.replace(url); // ぱくり1 /**************************************************************************/ /************************* Class Identifier *******************************/ /**************************************************************************/ function Identifier(baseURI, id){ /* Handle URI, URL and OS-paths as identifier. Copy left. Comment? w650s@mcn.ne.jp */ this.__base = baseURI; this.__id = (id !== undefined)? id : ''; this.__cache = null; } function IdentifierManager(/*Identifier.prototype*/self) { self.changeID = function(id){ this.__cache = null; this.__id = id; }; self.changeBase = function(URI){ this.__base = this.resolve(URI); }; self.getscheme = function(){ return this.getcomponents()[0]; }; self.getauthority = function(){ return this.getcomponents()[1]; }; self.getpath = function(){ return this.getcomponents()[2]; }; self.getquery = function(){ return this.getcomponents()[3]; }; self.getfragment = function(){ return this.getcomponents()[4]; }; self.getfilename = function(){ return this.getpath().split('/').pop(); }; self.__import__ = function(){ for (var i=0,len=arguments.length; i<len; i++) { var html = '<script type="text/javascript" src="'+ this.resolve(arguments[i]+'.js')+'"><\/script>'; document.write(html); } }; self.getcomponents = function(){ /* => [scheme, authority, path, query, fragment] */ var cache; if (cache = this.__cache) return cache[1]; cache = []; var uri = this.resolve(); REG_URI.test(uri); var scheme = RegExp.$1, authority = RegExp.$2, path = RegExp.$3, query = '', fragment = '', i; if ((i = path.indexOf('#')) != -1) { fragment = path.slice(i+1, path.length); path = path.slice(0, i); } if ((i = path.indexOf('?')) != -1) { query = path.slice(i+1, path.length); path = path.slice(0, i); } var components = [scheme, authority, path, query, fragment]; cache[0] = this.__id, cache[1] = components; this.__cache = cache; return components; }; self.resolve = function(/*optional*/relativeURI){ var ruri = (relativeURI === undefined)? this.__id : relativeURI; if (/^[\#\?]/.test(ruri) || ruri == '') return this.__base + ruri; if (ruri.match(REG_PATH)) return pathToUri(); if (REG_URI.test(ruri)) return ruri; var buri = this.__base; if (buri.match(REG_PATH)) buri = pathToUri(); REG_URI.test(buri); var scheme = RegExp.$1, authority = RegExp.$2, path = RegExp.$3; if (ruri.slice(0,2) == '//') return scheme + ':' + ruri; if (ruri.charAt(0) == '/') return scheme + '://' + authority + ruri; var i = path.lastIndexOf('/'); path = (i != -1)? path.slice(0, i) : ''; if (ruri.indexOf('./') == 0) ruri = ruri.slice(2, ruri.length); var components = [scheme+':/', authority]; while (ruri.indexOf('../') == 0 && path.length) { ruri = ruri.slice(3, ruri.length); path = ((i = path.lastIndexOf('/', path.length-2)) != -1)? path.slice(0, i) : ''; } if (path) components.push(path); components.push(ruri); return components.join('/'); function pathToUri(){ return ['file://', RegExp.$1, RegExp.$2.replace(/\\/g, '/')].join('/'); } }; // private, final variables and its getters var REG_PATH = /^([a-zA-Z]:)\\([^\s]+)/; self.getRePath = function(){ return REG_PATH; }; var REG_URI = /^([a-zA-Z0-9]+):\/\/(\/?[^\s\/]+)\/(.*)/; self.getReURI = function(){ return REG_URI; }; } IdentifierManager(Identifier.prototype); // ぱくり2 /* || Cookie Management Class (cookie_manager.js) || Script Version: 0.1.0 || Copyright (C) 2004 Hawk <w3l_admin@hawk.34sp.com> || WebSite URL: http://www.hawk.34sp.com/ */ /** * @static * @access public * @param int * @param string * @return Date * */ Date.fromNow = function(num, u) { var units = { 'Y':'FullYear', 'M':'Month', 'D':'Date', 'h':'Hours', 'm':'Minutes', 's':'Seconds' }; if(units[u]==null) return null; var d = new Date(); d['set'+ units[u]](d['get'+ units[u]]() + num); return d; } /** * class CookieManager * * */ function CookieManager () { /** * @static * @access private * */ var id = new Object(); var cookies = {}; var past = Date.fromNow(-1, 'Y'); /** * [Constructor] * * @param Date * @param string * @param string * @param bool * */ CookieManager = function (_expires, _path, _domain, _secure) { this.expires= _expires!=null ? _expires: CookieManager.expires; this.path = _path !=null ? _path : CookieManager.path; this.domain = _domain !=null ? _domain : CookieManager.domain; this.secure = _secure !=null ? _secure : CookieManager.secure; var privates = {}; this._getPrivate = function(o, name) { if(o==id) return privates[name]; else undefined; } this._setPrivate = function(o, name, value) { if(o==id) privates[name] = value; } } //initializing static members ... /** * @static * @access public * */ CookieManager.expires= null;//:Date CookieManager.path = null;//:string CookieManager.domain = null;//:string CookieManager.secure = false;//:bool /** * @static * @access private * */ function readCookies() { cookies = {}; var temp = []; if(document.cookie != '') { var datas= document.cookie.split("; "); for(var i=0; i<datas.length; i++){ temp =datas[i].split("="); cookies[temp[0]]=unescape(temp[1]); } } } readCookies(); /** * @static * @access public * @param string key * @param string value * */ CookieManager.setCookie = function(key, value) { var temp = key +"="+ escape(value); if(this.expires != null) temp+="; expires="+ this.expires.toGMTString(); if(this.path != null) temp+="; path=" + this.path; if(this.domain != null) temp+="; domain=" + this.domain; if(this.secure) temp += "; secure"; document.cookie=temp; readCookies(); } /** * @static * @access public * @param string key * @param string defValue * @return string * */ CookieManager.getCookie = function (key, defValue) { return cookies[key]!=null ? cookies[key] : defValue; } /** * @static * @access public * @param key * */ CookieManager.deleteCookie = function (key) { var tmp = this.expires; this.expires = past; this.setCookie(key, 'dummy'); this.expires = tmp; readCookies(); } /** * @access public * @param string key * @param string value * */ CookieManager.prototype.setCookie = CookieManager.setCookie; /** * @access public * @param string key * @param string defValue * @return string * */ CookieManager.prototype.getCookie = CookieManager.getCookie; /** * @access public * @param key * */ CookieManager.prototype.deleteCookie = CookieManager.deleteCookie; }; CookieManager(); // Cookieの扱いを設定 var URI = new Identifier(location.href); CookieManager.expires = Date.fromNow(1, 'h'); CookieManager.path = '/' + URI.getpath(); CookieManager.domain= URI.getauthority(); CookieManager.secure= true; // 渡されたFormの値をCookieに保存 // ただし,すべてのフォームに対応してない. function store(form) { CookieManager.setCookie('__isStore', true); e = form.elements; for(i = 0; i < e.length; ++i) { switch(e[i].type) { case 'text': if(e[i].value) CookieManager.setCookie(e[i].name, e[i].value); value = CookieManager.getCookie(e[i].name, false); alert(e[i].name + " -> " + value); break; case 'select-one': CookieManager.setCookie(e[i].name, e[i].selectedIndex); break; case 'radio': case 'checkbox': if(e[i].checked) CookieManager.setCookie(e[i].name, e[i].value); break; default: } } } // フォームの値を復元する // storeと同じく,すべてのフォームには対応してない. // それと,一回しか復元しない // できれば,Cookieも削除するほうがいいのだろうが,とりあえず,やっていない. // CookieManagerをもう少し改良してからのほうがいいでしょ,たぶん. function load(form) { if(!CookieManager.getCookie('__isStore', false)) return; CookieManager.deleteCookie('__isStore'); e = form.elements; for(i = 0; i < e.length; ++i) { key = e[i].name; value = CookieManager.getCookie(key, false); CookieManager.deleteCookie(key); if(!value) continue; alert(key + " -> " + value); switch(e[i].type) { case 'text': e[i].value = value; break; case 'select-one': e[i].options[value].selected = true; break; case 'radio': case 'checkbox': if(e[i].value == value) e[i].checked = true; break; default: } } } // 指定されたテキストボックスのメールアドレスをチェック // これもパクリ,ただし,どっからぱくったかわすれたw // ついでに,RFCには対応していないw // メッセージを表示して,フォーカスを設定する // プログラムが簡単になるようにfalseを返している function mail_check_fail(tb, msg) { alert(msg); tb.focus(); return false; } function mail_check(tb) { email = tb.value; atmark = 0; piriod = 0; if (email == '') return mail_check_fail(tb, "メールアドレスを入力してください。"); cc = email.charAt(0); if (((cc < "0") || ("9" < cc)) && ((cc < "a") || ("z" < cc)) && ((cc < "A") || ("Z" < cc))) return mail_check_fail(tb, "メールアドレスに不正があります。"); cc = email.charAt(email.length - 1); if (((cc < "0") || ("9" < cc)) && ((cc < "a") || ("z" < cc)) && ((cc < "A") || ("Z" < cc))) return mail_check_fail(tb, "メールアドレスに不正があります。"); for (i = 1;i < email.length; i++) { cc = email.charAt(i); if (cc == "@") { atmark++; continue; } if (cc == ".") { piriod++; continue; } if (((cc < "0") || ("9" < cc)) && ((cc < "a") || ("z" < cc)) && ((cc < "A") || ("Z" < cc)) && ("@" != cc) && ("." != cc) && ("_" != cc) && ("-" != cc)) return mail_check_fail(tb, "メールアドレスに不正があります。"); } if (atmark < 1) return mail_check_fail(tb, "メールアドレスに@が含まれていません。"); if (atmark > 1) return mail_check_fail(tb, "メールアドレスに@が多すぎます。"); if (piriod < 1) return mail_check_fail(tb, "メールアドレスにピリオドが含まれていません。"); return true; } // なんとなく function check_radio(r, msg) { for(i = 0; i < r.length; ++i) if(r[i].checked) return true; alert(msg + "を選択してください."); r[0].focus(); return false; } function get_radio_value(r) { for(i = 0; i < r.length; ++i) if(r[i].checked) return r[i].value; } //--> </SCRIPT> onSubmit とかで,store(document.Form) とかすればOK <form ... name="Form"> ・・・ </form> <SCRIPT LANGUAGE="JAVASCRIPT"> <!-- // 当たり前だけど,Formを作ってからじゃないと,エラーが発生する load(document.Form); //--> </SCRIPT>
意外と,これぐらいのJavaScriptだと大抵のブラウザでうごくっぽい
ただ,Operaでcheckboxにフォーカスする方法がちょっとわからん.
IEとFireFoxだとできたんだけどね...