[英]How to delete Cookie before redirect to other page in javascript?
I have this code to set up and check a cookie, and before a redirect (if the user click on the cancel button) I need to unset or delete the cookie. 我有此代码来设置和检查cookie,并且在重定向(如果用户单击“取消”按钮)之前,我需要取消设置或删除cookie。
function getCookie(c_name) {
if (document.cookie.length > 0) {
c_start = document.cookie.indexOf(c_name + "=")
if (c_start != -1) {
c_start = c_start + c_name.length + 1
c_end = document.cookie.indexOf(";", c_start)
if (c_end == -1) c_end = document.cookie.length
return unescape(document.cookie.substring(c_start, c_end))
}
}
return "";
}
function setCookie(c_name, value, expiredays) {
var exdate = new Date()
exdate.setDate(exdate.getDate() + expiredays)
document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + exdate.toGMTString())
}
function checkCookie() {
var todaysdate = new Date()
var day = todaysdate.getDay()
switch (day) {
case 1:
day = "Monday"
break
case 2:
day = "Tuesday"
break
case 3:
day = "Wednesday"
break
case 4:
day = "Thursday"
break
case 5:
day = "Friday"
break
case 6:
day = "Saturday"
break
case 0:
day = "Sunday"
break
}
var thedate = getCookie('thedate')
if (thedate != null && thedate != "") {
if (day == thedate) {} else {
alert('')
}
} else {
thedate = day
if (thedate != null && thedate != "") {
setCookie('thedate', thedate, 365)
// alert('dsadasdasdasdasdasdasd')
var answer = confirm("Please click on OK to continue loading my page, or CANCEL to be directed to the Yahoo site.")
if (!answer) {
window.location = "http://www.yahoo.com/";
}
}
}
}
How to unset the cookie c_name
? 如何取消设置cookie
c_name
?
I'm sure it's something easy, however I am not able to unset this cookie. 我敢肯定这很简单,但是我无法取消设置此Cookie。
Just set an empty cookie and set it to expire before today (ie -1
) : 只需设置一个空cookie并将其设置为在今天之前过期(即
-1
):
if (!answer) {
setCookie('thedate', '', -1); //using your already existing setCookie function
window.location="http://www.yahoo.com/";
}
function loaded()
{
document.cookie = "v0=1;";
document.cookie = "v1=2;";
alert(document.cookie);
}
function deletecook()
{
var d = new Date();
document.cookie = "v0=1;expires=" + d.toGMTString() + ";" + ";";
alert(document.cookie);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.