cookie对比localStorage哪个适合作为网站皮肤存储

cookie

cookie : 一般由服务器生成,可设置失效时间。如果在浏览器生成,默认是关闭浏览器之后失效

    存储大小:4k

    每次都会携带在HTTP头中,如果使用 cookie 保存过多数据会带来性能问题

    一般由服务器端生成,用于标识用户身份

缺点 : 原生的cookie用起来有点麻烦,如果用jQuery,则需要引入jquery库及jquery.cookie库

优点 : 兼容性比较好,对IE浏览器也很好兼容,可兼容IE6,厉害了我的哥

需要注意 :  本地支持cookie的浏览器有 : IE6+,火狐(目前测试过这些浏览器)

      不支持的浏览器有:360浏览器,谷歌浏览器,欧朋浏览器(目前测试过这些浏览器)

      但是把文件放到,浏览器中都有很好的支持

cookie调用方式

新添加一个会话cookie:

$.cookie('the_cookie', 'the_value');
注:当没有指明cookie有效时间时,所创建的cookie有效期默认到用户关闭浏览器为止,所以被称为“会话cookie(session cookie)” 创建一个cookie并设置有效时间为7天: $.cookie('the_cookie', 'the_value', { expires: 7 });
注:当指明了cookie有效时间时,所创建的cookie被称为“持久cookie(persistent cookie)”。 创建一个cookie并设置cookie的有效路径:
$.cookie('the_cookie', 'the_value', { expires: 7, path: '/' });
注:在默认情况下,只有设置cookie的网页才能读取该cookie。如果想让一个页面读取另一个页面设置的cookie,必须设置cookie的路径。
cookie的路径用于设置能够读取cookie的顶级目录。将这个路径设置为网站的根目录,可以让所有网页都能互相读取cookie(一般不要这样设置,防止出现冲突) 读取cookie:
$.cookie('the_cookie');
// cookie存在 => 'the_value' $.cookie('not_existing'); // cookie不存在 => null 删除cookie,通过传递null作为cookie的值即可:
$.cookie('the_cookie', null);

localStorage

LocalStorage : 除非被清楚,否则永久保存

    一般 5MB

    仅在客户端中保存,不参与和服务器的通信。

    用于浏览器端缓存数据

缺点 : 对IE浏览器有兼容性问题,官网说能兼容IE8+,可我的浏览器IE10也没有效果,到现在还未解决此问题,如果有人知道,请留言告知我,谢谢

优点 : 用起来非常简单,直接申明变量即可

需要注意 :经过测试:在本地中IE所有浏览器均不支持LocalStorage,其他浏览器都支持

      部署到服务器中,IE8及IE8以上都支持LocalStorage

如果对IE没有很高的要求建议用LocalStorage,如果对IE有高要求那么建议用LocalStorage,因人而异

原理 : 通过改变link中的href路径,达到主题切换效果

LocalStorage 调用方式

localStorage.setItem("key","value");//存储变量名为key,值为value的变量  

localStorage.key = "value"//存储变量名为key,值为value的变量  

localStorage.getItem("key");//获取存储的变量key的值www.it165.net  

localStorage.key;//获取存储的变量key的值  

localStorage.removeItem("key")//删除变量名为key的存储变量  

效果图:

点击不同的色块,字体会变成不一样的颜色

运行我的dom之前,请在当前目录下新建一个css文件,然后在css文件中键入5个css主题文件

css文件

样式内容

localStorage技术

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>theme - localStorage</title>
<!--<link rel="stylesheet" id = "theme" href="css/green.css">-->
<style>
*{margin: 0;padding: 0}
.theme-style{overflow: hidden;margin: auto;width: 300px;}
.theme-style li{display: inline-block;width: 50px;height: 50px;margin-right: 0.5em;float: left;cursor: pointer}
.box{width: 400px;margin: auto;line-height: 40px;}
.theme-style li:nth-child(1){background: red}
.theme-style li:nth-child(2){background: green}
.theme-style li:nth-child(3){background:yellow}
.theme-style li:nth-child(4){background: pink}
.theme-style li:nth-child(5){background: orange}
</style>
</head>
<body>
<ul id="theme-style" class="theme-style">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
<div class="box">
<p>上周五下午三点收到一个沙特客户钢制更衣柜的询价. 严格意义上也算个老客户,两年前下过一个订单. 当时收到货后还比较满意,曾经有意向要做沙特的代理,后来因为经济情况不了了之.</p>
</div>
</body>
</html>
<script>
//自动加载加入节点
var link = document.createElement('link');
var head = document.getElementsByTagName('head')[0];
link.rel = 'stylesheet';
link.id = 'theme';
localStorage.getItem('url') ? link.href = localStorage.getItem('url') : link.href = 'css/red.css';
head.appendChild(link) //获取内联样式
function getCss(obj,name){
if(obj.currentStyle) {
return obj.currentStyle[name];
}
else {
return document.defaultView.getComputedStyle(obj,null)[name];
}
} var arr = ['red' , 'green' , 'yellow' , 'pink' , 'orange'];
var themeStyle = document.getElementById('theme-style').getElementsByTagName('li');
var link = document.getElementById('theme');
//更换样式
for(var i = 0 ; i < themeStyle.length ; i++ ){
(function(number){
themeStyle[i].onclick = function(){
localStorage.setItem('url','css/'+arr[number]+'.css');
link.setAttribute('href',localStorage.getItem('url'));
}
})(i);
}
</script>

cookie技术

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>theme - localStorage</title>
<script src="js/jquery-3.2.1.min.js"></script>
<script src="js/jquery.cookie.js"></script>
<!--<link rel="stylesheet" id = "theme" href="css/green.css">-->
<style>
*{margin: 0;padding: 0}
.theme-style{overflow: hidden;margin: auto;width: 300px;}
.theme-style li{display: inline-block;width: 50px;height: 50px;margin-right: 0.5em;float: left;cursor: pointer}
.box{width: 400px;margin: auto;line-height: 40px;}
.theme-style li:nth-child(1){background: red}
.theme-style li:nth-child(2){background: green}
.theme-style li:nth-child(3){background:yellow}
.theme-style li:nth-child(4){background: pink}
.theme-style li:nth-child(5){background: orange}
</style>
</head>
<body>
<ul id="theme-style" class="theme-style">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
<div class="box">
<p>上周五下午三点收到一个沙特客户钢制更衣柜的询价. 严格意义上也算个老客户,两年前下过一个订单. 当时收到货后还比较满意,曾经有意向要做沙特的代理,后来因为经济情况不了了之.</p>
</div>
</body>
</html>
<script>
//自动加载加入节点
var link = document.createElement('link');
var head = document.getElementsByTagName('head')[0];
link.rel = 'stylesheet';
link.id = 'theme';
$.cookie('url') ? link.href = $.cookie('url') : link.href = 'css/red.css';
head.appendChild(link) //获取内联样式
function getCss(obj,name){
if(obj.currentStyle) {
return obj.currentStyle[name];
}
else {
return document.defaultView.getComputedStyle(obj,null)[name];
}
} var arr = ['red' , 'green' , 'yellow' , 'pink' , 'orange'];
var themeStyle = document.getElementById('theme-style').getElementsByTagName('li');
var link = document.getElementById('theme');
//更换样式
for(var i = 0 ; i < themeStyle.length ; i++ ){
(function(number){
themeStyle[i].onclick = function(){
$.cookie('url','css/'+arr[number]+'.css' , { expires: 30 })
link.setAttribute('href',$.cookie('url'));
}
})(i);
}
</script>

    

    

cookie对比localStorage哪个适合作为网站皮肤存储的更多相关文章

  1. 深入了解浏览器存储:对比Cookie、LocalStorage、sessionStorage与IndexedDB

    摘要: 对比Cookie.LocalStorage.sessionStorage与IndexedDB 作者:浪里行舟 Fundebug经授权转载,版权归原作者所有. 前言 随着移动网络的发展与演化,我 ...

  2. JS 详解 Cookie、 LocalStorage 与 SessionStorage

    基本概念 Cookie Cookie 是小甜饼的意思.顾名思义,cookie 确实非常小,它的大小限制为4KB左右.它的主要用途有保存登录信息,比如你登录某个网站市场可以看到"记住密码&qu ...

  3. Cookie、LocalStorage 与 SessionStorage的区别在哪里?

    基本概念 Cookie Cookie 是小甜饼的意思.顾名思义,cookie 确实非常小,它的大小限制为4KB左右.它的主要用途有保存登录信息,比如你登录某个网站市场可以看到“记住密码”,这通常就是通 ...

  4. session,cookie,sessionStorage,localStorage的区别及应用场景

    session,cookie,sessionStorage,localStorage的区别及应用场景 浏览器的缓存机制提供了可以将用户数据存储在客户端上的方式,可以利用cookie,session等跟 ...

  5. 缓存session,cookie,sessionStorage,localStorage的区别

    https://www.cnblogs.com/cencenyue/p/7604651.html(copy) 浅谈session,cookie,sessionStorage,localStorage的 ...

  6. js中cookie,localStorage(sessionStorage)的存取

    一.cookie (原生的不好用,自己简单封装) 1. 存cookie的方法: function setCookie(c_name,value,expiredays) { var exdate=new ...

  7. cookie和localStorage、sessionStorage的区别

    先来讲讲localStorage吧,我最初接触localStorage,是听一个同学说他在做项目的过程中用到过这个.但是我自己也用到过的,就是在学习React的时候,在做一个小demo,这个demo简 ...

  8. 什么是cookie(前段时间看到别人简历上把cookie和localStorage混淆了所以专门又去了解了下)

    在前端面试中,有一个必问的问题:请你谈谈cookie和localStorage有什么区别啊? localStorage是H5中的一种浏览器本地存储方式,而实际上,cookie本身并不是用来做服务器存储 ...

  9. 详解Cookie、LocalStorage、SessionStorage

    不管是笔试还是面试相信大家都会经常遇到问Cookie.LocalStorage.SessionStorage 这三个不同的,什么不说先上一波图先: 针对他们大小之分应用场景也有不同: 因为考虑到每个 ...

随机推荐

  1. 第八章 委托,lamdbda 表达式和事件

    第八章 委托,lamdbda 表达式和事件 委托是寻址方式的.net版本. 委托是类型安全的类,它定义了返回类型和参数的类型.委托类不仅包含方法的应用,也可以包含对多个方法的引用. 在 C++中,函数 ...

  2. route 命令

    Linux系统的route命令用于显示和操作IP路由表(show / manipulate the IP routing table).要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或 ...

  3. 4.关于QT中的QFile文件操作,QBuffer,Label上加入QPixmap,QByteArray和QString之间的差别,QTextStream和QDataStream的差别,QT内存映射(

     新建项目13IO 13IO.pro HEADERS += \ MyWidget.h SOURCES += \ MyWidget.cpp QT += gui widgets network CON ...

  4. Linux2_小技巧

    0 鼠标不灵么: 左侧设置图标----显示----未知显示屏--关闭 1 左侧自动隐藏 右键---更改桌面背景---行为--隐藏 2 终端打开 搜索到终端添加到左侧 ctrl+alt+T快捷打开 ct ...

  5. 登录shell与非登录shell读取文件过程

    登录shell与非登录shell读取文件过程登录:/etc/profile→/etc/profile.d/*.sh        ~/.bash_profile非登录:~/.bash_profile→ ...

  6. Android蓝牙通信具体解释

    蓝牙通信的大概过程例如以下: 1.首先开启蓝牙 2,搜索可用设备 3,创建蓝牙socket.获取输入输出流 4,读取和写入数据 5.断开连接关闭蓝牙 还要发送配对码发送进行推断! 以下是全部的源码:不 ...

  7. 【BZOJ】2186 沙拉公主的困惑

    一道很有价值的题. [解析1]欧几里德算法求乘法逆元,前缀和 [Analysis]O(T n log n). [Sum] ①int运算.假设会超出界,第一个数前要加上(LL)即类型转换. ②gcd不变 ...

  8. Android 快速开发系列 ORMLite 框架最佳实践之实现历史记录搜索

    首先在build.gald中添加compile 'com.j256.ormlite:ormlite-android:4.48'的引用 compile 'com.j256.ormlite:ormlite ...

  9. DevExpress控件显示前弹出到期时间解决办法

    方法是,删除Properties下的license.licx文件,重新生成即可.

  10. 【python】-- web开发之DOM

    DOM 文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文档的编程接口.它给文档提供了一种结构化的表示方法,可以改变文档的内容和呈现方式.我们最为关心的是, ...