<script> <!-- /* Coded By redsos @ 2005-12-27 Firfox/IE6 测试通过。 此方法的优点和缺点请查看 window.opener 属性的说明。 利用此方法可用作离线保存(缓存数据)。 opener.content可改换为: window.name , parent.name 或 self.name 以达到不同的效果。 */ function set(name,str){ if(typeof(opener.content)=="undefined") opener.content = new Array(); opener.content[name] = str; } function get(name){ if(typeof(opener.content)=="undefined" || typeof(opener.content[name])=="undefined") { return false; }else{ return opener.content[name]; } } function elm(obj){ return document.getElementById(obj); } //--> </script><strong>test:</strong><br /> <span id=view></span><br /> Name:<input type="text" id="name" /><br /><br /> Str :<input type="text" id="str" /> <input type=button value=set onclick=set(elm('name').value,elm('str').value) /> <input type=button value=get onclick=elm('view').innerHTML=get(elm('name').value) /> /* 另外一种方法。如果不考虑兼容性的话。使用IE的 UserData 是一个不错的方法。 Sample code 引用自 http://www.htmlforums.com 的 Jon Hanlon */ <html> <head> <title>Userdata</title> <style type="text/css"> .userData { behavior:url(#default#userdata); } </style> </head> <body> Instructions: Click inside the black box and enter some new text.<br> Save this data with the <i>Put Data</i> button.<br> Then reload, close down IE, reboot, whatever. Return to this page. Restore the saved data with the <i>Get Data</i> button. <br><br> <span class="userData" style="border: thin solid black" contenteditable="true" id="spnUserData"> What's new pussycat? </span> <br><br> <button onclick='putUserData(spnUserData,"myData","myText",spnUserData.innerText)' id='btnPut'><i>Put Data</i></button> <br> <button onclick='spnUserData.innerText=getUserData(spnUserData,"myData","myText")' id='btnGet'><i>Get Data</i></button> <script language="javascript"> function putUserData(oUD,sUDName,sName,sVal) { oUD.setAttribute(sName,sVal); oUD.save(sUDName); return; } function getUserData(oUD,sUDName,sName) { oUD.load(sUDName); return oUD.getAttribute(sName); } </script> </body> </html> |