好!欢迎访问迷津渡口 现在时间是: 天气 °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
   支持一下
挤眼 亲亲 咆哮 开心 想想 可怜 糗大了 委屈 哈哈 小声点 右哼哼 左哼哼 疑问 坏笑 赚钱啦 悲伤 耍酷 勾引 厉害 握手 耶 嘻嘻 害羞 鼓掌 馋嘴 抓狂 抱抱 围观 威武 给力
 点赞
 签到
 表情
 图片
 代码
提交评论

清空信息
关闭评论
dadees
 dadees 约 4 小时前回复
#75
<a href="http://www.praiseti.com/adidas-porsche-design-v-guld-scarped">adidas porsche design v guld</a> <a href="http://www.cynshq.com/nike-sb-stefan-janoski-max-sort-kaufen-lyrics-kurpesd">nike sb stefan janoski max sort kaufen lyrics</a> <a href="http://www.fnjcompressor.com/oakley-sunglasses-gauge-8-hybrid-sunglassesc">oakley sunglasses gauge 8 hybrid</a> <a href="http://www.kifftradingco.com/nfl-mens-carolina-panthers-nike-black-ko-wordmark-performance-hoodie-nflr">nfl mens carolina panthers nike black ko wordmark performance hoodie</a> <a href="http://www.phaqtory.com/orange-blue-mens-nike-air-max-97-shoes-skoa">orange blue mens nike air max 97 shoes</a> <a href="http://www.cialislaw.com/nike-air-max-tailwind-7-kvinners-bl%C3%A5-sneakere">nike air max tailwind 7 kvinners bl氓</a>
dadees http://www.dadees.com/
reigapfunding
 reigapfunding 约 21 小时前回复
#74
<a href="http://www.sinririnsyo.com/nike-air-max-tailwind-6-dam%C3%A4nner-schwarz-series-schuheg">nike air max tailwind 6 dam盲nner schwarz series</a> <a href="http://www.walkerhut.com/herren-ralph-lauren-london-olympic-schwarz-gelb-poloz">herren ralph lauren london olympic schwarz gelb</a> <a href="http://www.chrissinacori.com/nike-air-jordan-son-of-mars-wei%C3%9F-cem%C3%A4nnert-kits-chaussuresa">nike air jordan son of mars wei脽 cem盲nnert kits</a> <a href="http://www.coldlakeroof.com/air-jordan-retro-14-barn-gull-oransje-schuhee">air jordan retro 14 barn gull oransje</a> <a href="http://www.ericbarrak.com/air-jordan-11-plata-rosado-scarpea">air jordan 11 plata rosado</a> <a href="http://www.freemmark.com/adidas-ultra-boost-crystal-blanco-instructions-kurpesf">adidas ultra boost crystal blanco instructions</a>
[url=http://www.reigapfunding.com/]reigapfunding[/url]
lmcgroep
 lmcgroep 2019-02-15 04:22回复
#73
<a href="http://www.wuxiangtaichi.com/nike-kobe-9-kvinders-gul-himmelbl%C3%A5-skoi">nike kobe 9 kvinders gul himmelbl氓</a> <a href="http://www.culdesacmag.com/nike-zoom-pegasus-34-oransje-rosa-obuvq">nike zoom pegasus 34 oransje rosa</a> <a href="http://www.livinggreedy.com/nike-kobe-icon-green-grey-shoesh">nike kobe icon green grey</a> <a href="http://www.clickstroy.com/femminile-nike-air-max-lunar-1-blu-rosa-scarpei">femminile nike air max lunar 1 blu rosa</a> <a href="http://www.zotorrent.com/air-jordan-reveal-blau-minzgr%C3%BCn-schuhej">air jordan reveal blau minzgr眉n</a> <a href="http://www.viptarimas.com/minnesota-twins-new-era-mlb-leather-metal-plate-9fifty-snapback-cap-hath">minnesota twins new era mlb leather metal plate 9fifty snapback cap</a>
lmcgroep http://www.lmcgroep.com/
pearsonchool
 pearsonchool 2019-02-13 20:46回复
#72
<a href="http://www.jupickuplines.com/nike-lebron-witness-rosa-argento-scarpel">nike lebron witness rosa argento</a> <a href="http://www.saleuggboot.com/mens-nike-flynit-air-max-all-blue-shoesj">mens nike flynit air max all blue</a> <a href="http://www.fragmanizletv.com/nike-zoom-vomero-kvinders-r%C3%B8d-s%C3%B8lv-skoi">nike zoom vomero kvinders r酶d s酶lv</a> <a href="http://www.swisshomeas.com/femminile-nike-shox-turbo-21-arancia-blu-scarpeq">femminile nike shox turbo 21 arancia blu</a> <a href="http://www.marinersdiary.com/nike-zoom-pegasus-32-womens-sky-blue-purple-shoesj">nike zoom pegasus 32 womens sky blue purple</a> <a href="http://www.bourseasie.com/black-jet-set-michael-kors-wallet-mkh">black jet set michael kors wallet</a>
pearsonchool http://www.pearsonchool.com/
imannequins
 imannequins 2019-02-12 06:00回复
#71
<a href="http://www.myfraimz.com/adidas-neo-xpression-womens-shoes-coupon-shoesf">adidas neo xpression womens shoes coupon</a> <a href="http://www.winnercells.com/nike-light-pink-hat-hats">nike light pink hat</a> <a href="http://www.atppest.com/nike-air-max-90-youth-sneakere">nike air max 90 youth</a> <a href="http://www.windriftbar.com/asics-gel-sendai-2-performance-l%C3%B8b-sko-herre-pants-obuvd">asics gel sendai 2 performance l酶b sko herre pants</a> <a href="http://www.kidtokidboise.com/nike-mens-vita-vea-limited-orange-alternate-jersey-tampa-bay-buccaneers-nfl-50-nflx">nike mens vita vea limited orange alternate jersey tampa bay buccaneers nfl 50</a> <a href="http://www.mesmertronic.com/detroit-lions-jason-hanson-jersey-nflv">detroit lions jason hanson jersey</a>
imannequins http://www.imannequins.com/
webgiganten
 webgiganten 2019-02-11 07:51回复
#70
<a href="http://www.bidmani.com/air-jordan-4-brojo-cdp-chaussuresd">air jordan 4 brojo cdp</a> <a href="http://www.blackpointcpk.com/nike-free-run-2-gr%C3%B8n-gr%C3%A5-gr%C3%B8n-flash-butyg">nike free run 2 gr酶n gr氓 gr酶n flash</a> <a href="http://www.pyrpoint.com/adidas-superstar-2-w-argent-blanc-scarpea">adidas superstar 2 w argent blanc</a> <a href="http://www.seemeonweb.com/free-5.0-blau-laufen-schuhe-schuheb">free 5.0 blau laufen schuhe</a> <a href="http://www.bikajewels.com/air-jordan-10-retro-hvit-varsity-r%C3%B8d-svart-keys-butyf">air jordan 10 retro hvit varsity r酶d svart keys</a> <a href="http://www.clojuregames.com/nike-kd-7-gelb-schwarz-butyf">nike kd 7 gelb schwarz</a>
webgiganten http://www.webgiganten.com/
jlchatlaw
 jlchatlaw 2019-02-10 19:06回复
#69
<a href="http://www.acforlessllc.com/air-max-thea-dam%C3%A4nner-rosa-and-grau-obuvc">air max thea dam盲nner rosa and grau</a> <a href="http://www.thehodostore.com/nike-lavonte-david-red-name-number-logo-tampa-bay-buccaneers-nfl-54-long-sleeve-t-shirt-nflw">nike lavonte david red name number logo tampa bay buccaneers nfl 54 long sleeve t shirt</a> <a href="http://www.gandoff.com/detroit-lions-jason-hanson-jersey-nflv">detroit lions jason hanson jersey</a> <a href="http://www.langbenlang.com/air-jordan-1-retro-infrared-23-jordans-skog">air jordan 1 retro infrared 23 jordans</a> <a href="http://www.glennloovens.com/nike-hyperdunk-2014-low-womens-purple-gold-skob">nike hyperdunk 2014 low womens purple gold</a> <a href="http://www.kamilotomotiv.com/cincinnati-reds-bucket-hat-free-hata">cincinnati reds bucket hat free</a>
jlchatlaw http://www.jlchatlaw.com/
reddotsmedia
 reddotsmedia 2019-02-10 06:44回复
#68
<a href="http://www.entrustengage.com/nike-free-flyknit-3.0-l%C3%B8ping-sko-kvinners-svart-obuvd">nike free flyknit 3.0 l酶ping sko kvinners svart</a> <a href="http://www.frissedenkers.com/kvinners-nike-free-og-14-hvit-scarpea">kvinners nike free og 14 hvit</a> <a href="http://www.jauntycouture.com/air-jordan-retro-7-blau-silber-chaussuresg">air jordan retro 7 blau silber</a> <a href="http://www.myshinnies.com/jordan-2-gr%C3%BCn-candy-pack-kurpesf">jordan 2 gr眉n candy pack</a> <a href="http://www.thecreditgods.com/nike-flyknit-lunar-3-for-vendita-las-vegas-obuva">nike flyknit lunar 3 for vendita las vegas</a> <a href="http://www.zinoviaenache.com/ralph-lauren-tracksuit-hvid-gul-polox">ralph lauren tracksuit hvid gul</a>
reddotsmedia http://www.reddotsmedia.com/
ispardmore
 ispardmore 2019-02-09 17:48回复
#67
<a href="http://www.wuxiangtaichi.com/nike-air-flight-89-rouge-or-chaussuresj">nike air flight 89 rouge or</a> <a href="http://www.culdesacmag.com/nike-air-max-90-vt-orangen-blau-schuhej">nike air max 90 vt orangen blau</a> <a href="http://www.livinggreedy.com/nike-air-max-97-kvinders-r%C3%B8d-sort-skok">nike air max 97 kvinders r酶d sort</a> <a href="http://www.clickstroy.com/nike-free-run-dark-blu-arancia-scarpeh">nike free run dark blu arancia</a> <a href="http://www.zotorrent.com/maschio-nike-air-huarache-nm-arancia-oro-scarpei">maschio nike air huarache nm arancia oro</a> <a href="http://www.viptarimas.com/russell-westbrook-black-jersey-nflh">russell westbrook black jersey</a>
ispardmore http://www.ispardmore.com/
bibleofpeace
 bibleofpeace 2019-02-09 10:35回复
#66
<a href="http://www.redstonecloud.com/nike-kd-7-gelb-ribbon-obuvc">nike kd 7 gelb ribbon</a> <a href="http://www.thepoolfloats.com/kd-7-cave-violet-underwear-scarpea">kd 7 cave violet underwear</a> <a href="http://www.tsgctt.com/nike-dunk-oxford-rosa-and-wei%C3%9F-scarpee">nike dunk oxford rosa and wei脽</a> <a href="http://www.virginiaflyin.com/new-blance-574-kinder-gr%C3%BCn-braun-obuvc">new blance 574 kinder gr眉n braun</a> <a href="http://www.bandbwhitby.com/nike-kobe-11-rosa-svart-scarpea">nike kobe 11 rosa svart</a> <a href="http://www.funkymediamx.com/air-jordan-future-premium-infrarosso-wave-chaussuresg">air jordan future premium infrarosso wave</a>
bibleofpeace http://www.bibleofpeace.com/
顶部留言底部
 00:00/00:00
我想和你虚度时光 - 花房姑娘(9)
  1. 我想和你虚度时光
  2. 花房姑娘
  3. 恋恋风尘
  4. 我喜爱一切不彻底的事物
  5. 我的心里是满的
  6. Long Way
  7. 给少年的歌
  8. 晴日共剪窗
  9. 天上的月你的脸