Javascript删除cookie,然后重新加载或重定向

编程入门 行业动态 更新时间:2024-10-09 23:16:10
本文介绍了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; path = /'; window.location.href =www.example/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; path=/'; window.location.href = "www.example/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,然后重新加载或重定向

本文发布于:2023-10-12 05:12:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483812.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   加载   Javascript   cookie

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!