Javascript删除cookie,然后重新加载或重定向 [英] Javascript delete cookie before reload or redirect

查看:566
本文介绍了Javascript删除cookie,然后重新加载或重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要删除Cookie,然后 执行重定向。但是,在处理重定向之前,cookie不会被删除。问题是如果在执行重定向时cookie仍然存在,则重定向被拦截并发送到除了预期的页面之外的页面。 (奇怪,我知道;长的故事)



是否有可能欺骗浏览器并强制删除重定向之前的cookie?

  jQuery('div#panelD')click(function(){
document.cookie ='sharedsession =; expires = Thu,01 Jan 1970 00:00:01 GMT; Domain = .example.com; path = /';
window.location.href =www.example.com/x;
});我发现如果我对任何事情执行AJAX调用,那么我们可以通过调用这个函数来调用这个函数。它符合删除cookie的目的刷新。

  var fakeAjax = new XMLHttpRequest 
var anything = fakeAjax.responseText;
fakeAjax.open(GET,ajax_info.txt,false); //文件实际上不存在
fakeAjax.send();

请注意在空行中的false。异步必须设置为false(或一些其他延迟),以允许新的信息回来和cookie被删除的时间。



更新: IE不喜欢从不存在的文件请求responseText,所以只是完全删除该行。其他浏览器似乎很好,有或没有。


I need to delete a cookie and then do a redirect. However the cookie doesn't get deleted until the redirect is processed. The problem is that if the cookie still exists at the time the redirect is executed, the redirect is intercepted and sent to a page other than the one intended. (weird, I know; long story)

Is it possible to trick the browser and force the deletion of the cookie before the redirect?

jQuery('div#panelD').click(function(){
    document.cookie = 'sharedsession=; expires=Thu, 01 Jan 1970 00:00:01 GMT; Domain=.example.com; path=/';
    window.location.href = "www.example.com/x";
});

解决方案

I discovered if I perform an AJAX call for anything, it qualifies as a refresh for the purposed of deleting cookies.

var fakeAjax = new XMLHttpRequest();
var anything = fakeAjax.responseText;
fakeAjax.open("GET","ajax_info.txt",false);  // file doesn't actually exist
fakeAjax.send();

Note the "false" in the Open line. Asynchronous has to be set to false (or some other delay) to allow time for the new info to come back and the cookie to be deleted.

UPDATE: IE doesn't like requesting responseText from a file that doesn't exist so just remove that line completely. Other browsers seem to be fine with or without it.

这篇关于Javascript删除cookie,然后重新加载或重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆