zl程序教程

您现在的位置是:首页 >  后端

当前栏目

JSlocalStorage实现本地缓存的方法

方法缓存 实现 本地
2023-06-13 09:15:02 时间

复制代码代码如下:

<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html>
 <head>
   <title>本地缓存</title>
   <scripttype="text/javascript">
       varstrKey="strKey";
       varstorage=window.localStorage;

       functiononStart(){
           if(storage.getItem(strKey)!=null){
               alert(storage.getItem(strKey)+"localStorage");
           }elseif(Cookie.read(strKey)!=null){
               alert(Cookie.read(strKey)+"cookie");
           }
       }
       functionbendihuancun(){
           varstrValue=document.getElementById("username").value;
           if(storage){
           storage.setItem(strKey,strValue);  
       }else{
           Cookie.write(strKey,strValue); 
       }
       }
   </script>
 </head>
 <bodyonload="onStart()">
   <inputtype="text"id="username"value="123">
   <inputtype="button"value="保存缓存"onclick="bendihuancun()">
 </body>
</html>