Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
500 views
in Technique[技术] by (71.8m points)

javascript - 修改Cookie到期日期Javascript不起作用(Modify Cookie Expiration Date Javascript not working)

I have an html file which I am running on wampp server.

(我有一个在wampp服务器上运行的html文件。)

Below is structure of html file.

(以下是html文件的结构。)

 <!DOCTYPE html> <html lang="en"> <head> <script src="some_script.js"></script> // added a js library which is setting a cookie named "abc" with expiry date "2038" </head> <body> <div></div> </body> <script type="text/javascript" src="./one_more_script.js"></script> </html> 

Below is code in one_more_script.js - Here I am trying to change cookie expiry time to 380 days

(下面是one_more_script.js中的代码-在这里我试图将cookie到期时间更改为380天)

 window.onload = function(){ function createCookie(name,value) { var date = new Date(); date.setTime(date.getTime()+(380*24*60*60*1000)); var expires = "; expires="+date.toGMTString(); document.cookie = name+"="+value+expires+"; path=/"; } var myCookieValue; var cookieArr = document.cookie.split(";"); for(var i = 0; i < cookieArr.length; i++) { console.log(cookieArr[i]); var cookiePair = cookieArr[i].split("="); if('abc' == cookiePair[0].trim()) { myCookieValue = decodeURIComponent(cookiePair[1]); } } function eraseCookieFromAllPaths(name) { var pathBits = location.pathname.split('/'); var pathCurrent = ' path='; document.cookie = name + '=; expires=Thu, 01-Jan-1970 00:00:01 GMT;'; for (var i = 0; i < pathBits.length; i++) { pathCurrent += ((pathCurrent.substr(-1) != '/') ? '/' : '') + pathBits[i]; document.cookie = name + '=; expires=Thu, 01-Jan-1970 00:00:01 GMT;' + pathCurrent + ';'+"domain=localhost"; } } eraseCookieFromAllPaths("abc"); createCookie("abc",myCookieValue); } 

This code works in chrome console (developer tool).

(此代码在chrome控制台(开发人员工具)中有效。)

But while running through script it does not work.After page refresh cookie "abc" is set with expiry date "2038".

(但是在通过脚本运行时它不起作用。页面刷新cookie后设置的“ abc”的有效期为“ 2038”。)

  ask by nishi translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...