cookie的操作

 

比如这样如果一个网站上有两个域名的时候,我们需要考虑,两个域名下的cookie.

我们所说的跨域只的就是lesport.com和le.com 

js的cookie是不能跨域的,为了防止过大。

js增加删除和查找网上一大堆。下面有推荐的。

我们需要明白增加的时候,比如服务器已经有返回的cookie,我们在前端需要临时增加一个,一定要添加在一级域名下(二级域名也可以添加)

 

var Cookie = {
set: function(name, value, opt){
opt || (opt = {});
var t = new Date(), exp = opt.exp;
if(typeof exp==='number'){
t.setTime(t.getTime() + exp*3600000); //60m * 60s * 1000ms
}else if(exp==='forever'){
t.setFullYear(t.getFullYear()+50); //专业种植cookie 50年
}else if(value===null){ //删除cookie
value = '';
t.setTime(t.getTime() - 3600000);
}else if(exp instanceof Date){ //传的是一个时间对象
t = exp;
}else{
t = '';
}
document.cookie = name+'='+encodeURIComponent(value)+(t && '; expires='+t.toUTCString())+
'; domain='+(opt.domain || '.le.com')+'; path='+(opt.path || '/')+(opt.secure ? '; secure' : '');
},
get: function(name){
name += '=';
var cookies = (document.cookie || '').split(';'),
cookie,
nameLength = name.length,
i = cookies.length;
while(i--){
cookie = cookies[i].replace(/^\s+/,'');
if(cookie.slice(0,nameLength)===name){
return decodeURIComponent(cookie.slice(nameLength)).replace(/\s+$/,'');
}
}
return '';
}
};
function cookieTool(name, value, options) {
if(typeof value==='undefined'){
return Cookie.get(name);
}
if(options){
options.exp = typeof options.expires==='number' ? options.expires * 24 :
options.expires; //原来的cookie是按天算的
}
Cookie.set(name, value, options);
}
function getQuery(key, url) {
var urlArgs = url || location.search;
if (urlArgs.length > 0 && urlArgs.indexOf("?") != -1) {
var regex = new RegExp(key + "=([^&]*)", "i"),
result = urlArgs.match(regex);
return (result && result.length > 0) ? unescape(result[1]) : null;
}
return null;
}
function handler(data){
if(data && data.vast){
var cuid = data.vast.cuid;
var le_cookie = cookieTool('ark_uuid');
if(location.host.indexOf('.le.com') >= 0 && cuid && (!le_cookie || le_cookie != cuid)){
cookieTool('ark_uuid',cuid,{
'expires':new Date(2060,00,01, 08,00,01),
'domain':'.le.com'
});
//广告用户cookie设置到le.com成功后发送检测
var srcType = '';
var purl = getQuery('purl',location.href) || '';
if(purl.indexOf('.lesports.com') > 0)
srcType = 'http://ark.letv.com/apsdbg/cm/sport?url=';
else
srcType = 'http://ark.letv.com/apsdbg/cm/cloud?url=';

var img = document.createElement("img");
img.src = srcType + encodeURIComponent(purl);
img.onload = function(){
img = null;
}
}
}
};
var url = 'http://ark.letv.com/s?ark=0&ct=0&n=0&res=jsonp&j=handler&callback=handler';
var script = document.createElement('script');
script.setAttribute('src', url);
document.getElementsByTagName('head')[0].appendChild(script);

 

posted @ 2016-05-31 19:11  飘然离去  阅读(162)  评论(0编辑  收藏  举报