好!欢迎访问迷津渡口 现在时间是: 天气 °C ~ °C

迷津渡口

越感到迷惑 越接近真理

javascript写入读取清除cookie协助函数

cookie是记录访客足迹及留住访客的良好手段之一,但是处理cookie还是挺麻烦的,下面小指分享三个处理cookie的协助函数,你可以保存在类似cookie.js的文件里,然后调用即可使用这三个神奇的函数了。

首先了解一下cookie的结构:cookie是以键值对的形式保存的,即key=value的格式,各个cookie之间一般是以“;”分隔。


  1. writeCookie(name,value,days):写入cookie

    name为cookie键名,value为cookie键值,days为天数,可选,不输入则cookie在网页结束后就失效了。

  2. readCookie(name):读取cookie

    name为cookie键名,返回cookie键值

  3. eraseCookie(name):清除cookie

    name为cookie键名,清除方法为设为空值并有效期为过去-1天。


function writeCookie(name, value, days) {
  // By default, there is no expiration so the cookie is temporary
  var expires = "";

  // Specifying a number of days makes the cookie persistent
  if (days) {
    var date = new Date();
    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    expires = "; expires=" + date.toGMTString();
  }

  // Set the cookie to the name, value, and expiration date
  document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
  // Find the specified cookie and return its value
  var searchName = name + "=";
  var cookies = document.cookie.split(';');
  for(var i=0; i < cookies.length; i++) {
    var c = cookies[i];
    while (c.charAt(0) == ' ')
      c = c.substring(1, c.length);
    if (c.indexOf(searchName) == 0)
      return c.substring(searchName.length, c.length);
  }
  return null;
}

function eraseCookie(name) {
  // Erase the specified cookie
  writeCookie(name, "", -1);
}

另外最好在使用cookie的使用采用if (navigator.cookieEnabled)检测客户端是否支持cookie。

本文为小指的书摘,有需要的欢迎转载,记得点赞哦~

点赞1
   支持一下
挤眼 亲亲 咆哮 开心 想想 可怜 糗大了 委屈 哈哈 小声点 右哼哼 左哼哼 疑问 坏笑 赚钱啦 悲伤 耍酷 勾引 厉害 握手 耶 嘻嘻 害羞 鼓掌 馋嘴 抓狂 抱抱 围观 威武 给力
 点赞
 签到
 表情
 图片
 代码
提交评论

清空信息
关闭评论
ayuinc
 ayuinc 约 11 小时前回复
#166
<a href="http://www.unefinemouche.com/coach-poppy-sequin-spotlight-in-gold-coachh">coach poppy sequin spotlight in gold</a> <a href="http://www.ayuinc.com/nike-air-max-95-womens-white-pink-shoesj">nike air max 95 womens white pink</a> <a href="http://www.unirlisboa.com/kvinders-nike-free-tr-fit-gr%C3%A5-lilla-skol">kvinders nike free tr fit gr氓 lilla</a> <a href="http://www.escortfeeds.com/hommes-nike-zoom-pegasus-32-vert-bleu-chaussuresq">hommes nike zoom pegasus 32 vert bleu</a> <a href="http://www.rdjanitorial.com/air-jordan-jumpman-gris-bleu-ciel-chaussuresl">air jordan jumpman gris bleu ciel</a> <a href="http://www.bookoobeats.com/nike-air-yeezy-2-orangen-silber-schuhei">nike air yeezy 2 orangen silber</a>
ayuinc http://www.ayuinc.com/
dastforoush
 dastforoush 约 15 小时前回复
#165
<a href="http://www.amlakmehr.com/nike-dunk-sky-high-mesh-noir-chaussuresh">nike dunk sky high mesh noir</a> <a href="http://www.oneillbagwell.com/air-max-bw-ultra-cool-grau-wei%C3%9F-schuheh">air max bw ultra cool grau wei脽</a> <a href="http://www.ishidesign.com/nike-huarache-womens-white-and-black-shoesk">nike huarache womens white and black</a> <a href="http://www.qdoritsharon.com/alle-r%C3%B8d-nike-cortez-herre-skoj">alle r酶d nike cortez herre</a> <a href="http://www.conniesegal.com/grau-adidas-basketballe-schuhe-schuheh">grau adidas basketballe schuhe</a> <a href="http://www.mickieknudsen.com/nike-air-max-90-snow-leopard-black-sail-shoesk">nike air max 90 snow leopard black sail</a>
dastforoush http://www.dastforoush.com/
jeffaudette
 jeffaudette 2019-04-19 03:49回复
#164
<a href="http://www.rexpousa.com/converse-scarpe-for-girls-nero-scarpei">converse scarpe for girls nero</a> <a href="http://www.dalvadesign.com/miami-heat-beanie-hath">miami heat beanie</a> <a href="http://www.velvetwifi.com/nike-air-pegasus-83-femmes-bleu-chaussuresh">nike air pegasus 83 femmes bleu</a> <a href="http://www.sophiarugby.com/adidas-adv-eqt-rose-chaussuresj">adidas adv eqt rose</a> <a href="http://www.thumbglovepro.com/herren-nike-internationalist-wei%C3%9F-schuhek">herren nike internationalist wei脽</a> <a href="http://www.ceracustica.com/retro-tampa-bay-lightning-jersey-nflh">retro tampa bay lightning jersey</a>
jeffaudette http://www.jeffaudette.com/
sheeradiance
 sheeradiance 2019-04-18 20:12回复
#163
<a href="http://www.investicrypto.com/air-jordan-retro-5-anti-fur-gris-blanc-chaussuresl">air jordan retro 5 anti fur gris blanc</a> <a href="http://www.nattaravnur.com/adidas-yeezy-boost-350-pirate-schwarz-2016-schuhej">adidas yeezy boost 350 pirate schwarz 2016</a> <a href="http://www.uacheap.com/dam%C3%A4nner-nike-air-max-invigor-print-schwarz-schuhei">dam盲nner nike air max invigor print schwarz</a> <a href="http://www.sheeradiance.com/nike-lebron-witness-i-cemennt-hvit-obuvj">nike lebron witness i cemennt hvit</a> <a href="http://www.solclimasur.com/nike-sb-dunk-high-blu-scarpel">nike sb dunk high blu</a> <a href="http://www.successlms.com/air-jordan-6-infrarouge-1991-six-chaussuresh">air jordan 6 infrarouge 1991 six</a>
sheeradiance http://www.sheeradiance.com/
grossavind
 grossavind 2019-04-18 09:30回复
#162
<a href="http://www.sguerradesign.com/womens-air-jordan-retro-11-yellow-red-shoesj">womens air jordan retro 11 yellow red</a> <a href="http://www.zeneventsja.com/michael-kors-jet-set-large-crossbody-bag-mkh">michael kors jet set large crossbody bag</a> <a href="http://www.adharaweb.com/nike-air-max-tailwind-6-kvinners-r%C3%B8d-gul-obuvi">nike air max tailwind 6 kvinners r酶d gul</a> <a href="http://www.jrelselaw.com/femminile-nike-kd-7-nero-bianca-scarpej">femminile nike kd 7 nero bianca</a> <a href="http://www.intellalegal.com/nike-free-blue-lagoon-shoesh">nike free blue lagoon</a> <a href="http://www.carmelscoffee.com/air-jordan-retro-28-rosado-amarillo-zapatosp">air jordan retro 28 rosado amarillo</a>
[url=http://www.grossavind.com/]grossavind[/url]
skonya
 skonya 2019-04-18 02:45回复
#161
<a href="http://www.reviewtogel.com/adidas-tubular-runner-west-mesh-bleu-chaussuresl">adidas tubular runner west mesh bleu</a> <a href="http://www.igrejamormon.com/frau-air-jordan-retro-11-low-wei%C3%9F-schwarz-schuhej">frau air jordan retro 11 low wei脽 schwarz</a> <a href="http://www.skonya.com/nike-air-max-thea-schwarz-rot-schuhei">nike air max thea schwarz rot</a> <a href="http://www.monsterplate.com/nike-flyknit-racer-svart-hvit-on-feet-obuvj">nike flyknit racer svart hvit on feet</a> <a href="http://www.kentimpendik.com/femminile-nike-grigio-air-max-scarpel">femminile nike grigio air max</a> <a href="http://www.attivopizza.com/nike-air-max-1-essential-noir-gum-chaussuresh">nike air max 1 essential noir gum</a>
skonya http://www.skonya.com/
jobstodayng
 jobstodayng 2019-04-17 19:37回复
#160
<a href="http://www.mistopup.com/nike-air-max-98-maschio-marina-militare-blu-bianca-scarpeh">nike air max 98 maschio marina militare blu bianca</a> <a href="http://www.sporbahispro.com/chris-paul-throwback-jersey-nfli">chris paul throwback jersey</a> <a href="http://www.kalrakis.com/nike-flynit-max-mujer-releases-broen-mago-negro-verde-zapatosh">nike flynit max mujer releases broen mago negro verde</a> <a href="http://www.xinyudaili.com/nike-air-pegasus-83-oro-rosado-zapatosk">nike air pegasus 83 oro rosado</a> <a href="http://www.marieluwasche.com/north-face-denali-temperature-range-northfaceh">north face denali temperature range</a> <a href="http://www.ferrepress.com/nike-air-max-1-wei%C3%9F-kinder-schuhek">nike air max 1 wei脽 kinder</a>
jobstodayng http://www.jobstodayng.com/
cleanopoly
 cleanopoly 2019-04-17 08:17回复
#159
<a href="http://www.facenextday.com/air-jordan-cp3-gr%C3%A5-lyser%C3%B8d-butye">air jordan cp3 gr氓 lyser酶d</a> <a href="http://www.mellowsblues.com/adidas-pr%C3%B8dator-absolion-fg-instinct-schuhea">adidas pr酶dator absolion fg instinct</a> <a href="http://www.stackdotnet.com/nike-free-hypervenom-junior-footbtutti-scarpa-chaussuresa">nike free hypervenom junior footbtutti scarpa</a> <a href="http://www.donatelia.com/salomon-fell-raiser-or-orange-obuva">salomon fell raiser or orange</a> <a href="http://www.jafcosupply.com/nike-air-yeezy-2-oro-negro-rojo-butyd">nike air yeezy 2 oro negro rojo</a> <a href="http://www.kanaldtvizle.com/nike-ni%C3%B1os-zapatos-rojo-gris-scarpec">nike ni帽os zapatos rojo gris</a>
cleanopoly http://www.cleanopoly.com/
moviezluv
 moviezluv 2019-04-17 05:36回复
#158
<a href="http://www.agriind.com/olive-green-nike-air-max-plus-skow">olive green nike air max plus</a> <a href="http://www.rapheroes.com/mackage-odessa-coat-mackager">mackage odessa coat</a> <a href="http://www.odubmusic.com/billig-nike-free-run-performance-dam%C3%A4nner-blau-skod">billig nike free run performance dam盲nner blau</a> <a href="http://www.kidbleach.com/california-golden-bears-8-rodgers-blue-jersey-nfld">california golden bears 8 rodgers blue jersey</a> <a href="http://www.shoppula.com/versace-medusa-chain-belt-beltr">versace medusa chain belt</a> <a href="http://www.kanpourakuen.com/ray-ban-rb3016-clubmaster-sunglasses-mock-tortoise-arista-lt-sunglassesb">ray ban rb3016 clubmaster sunglasses mock tortoise arista lt</a>
moviezluv http://www.moviezluv.com/
laflammesinc
 laflammesinc 2019-04-16 23:27回复
#157
<a href="http://www.bgaddiict.com/north-face-vest-womens-sale-northfaceh">north face vest womens sale</a> <a href="http://www.deriogolf.com/nike-free-run-5.0-women-carbon-grey-shoesh">nike free run 5.0 women carbon grey</a> <a href="http://www.poluscompany.com/hommes-adidas-nmd-runner-bleu-chaussuresl">hommes adidas nmd runner bleu</a> <a href="http://www.pariswebjob.com/adidas-zx-750-leather-black-shoesj">adidas zx 750 leather black</a> <a href="http://www.dakvideo.com/dallas-mavericks-new-era-nba-2-tone-gray-black-59fifty-cap-hatk">dallas mavericks new era nba 2 tone gray black 59fifty cap</a> <a href="http://www.freshkeema.com/nike-air-presto-br-unisex-dark-bl%C3%A5-305919-002-skoj">nike air presto br unisex dark bl氓 305919 002</a>
laflammesinc http://www.laflammesinc.com/
顶部留言底部
 00:00/00:00
我想和你虚度时光 - 花房姑娘(9)
  1. 我想和你虚度时光
  2. 花房姑娘
  3. 恋恋风尘
  4. 我喜爱一切不彻底的事物
  5. 我的心里是满的
  6. Long Way
  7. 给少年的歌
  8. 晴日共剪窗
  9. 天上的月你的脸