用于清除cookie并重定向到html页面的Html按钮 [英] Html button that clears cookies and redirects to an html page

查看:159
本文介绍了用于清除cookie并重定向到html页面的Html按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找不同的解决方案,但你们中的任何人都知道简单的方法
写一个多按钮,清除cookie并重定向到某个页面

Ive been looking at different solutions but do any of you guys know the easy way to writting a multiple button that clears cookies and redirects to a certain page

我已经走到这一步了:

<script type="text/javascript">

$(document).ready(function(){
    deleteAllCookies();
});

现在我有一个链接,我只想让它刷新实际页面,清除cookie,以及然后重定向到另一个页面。

right now I got a link where i just want it to refresh the actual page, clear cookies, and then redirect to another page.

有没有简单的方法呢?

推荐答案

这是刷新后无法运行进程,您无法知道页面是如何加载的。
但是如果要清除所有cookie而不是重定向到另一个页面,

It's impossible to run a process after refresh, you can't know how is page loaded. But if you want to clear all cookies than redirect to another page,

deleteAllCookies()函数派生自 Robert J. Walker回答

deleteAllCookies() function derived from Robert J. Walker answer,

function deleteAllCookies() {
    var cookies = document.cookie.split(";");

    for (var i = 0; i < cookies.length; i++) {
        var cookie = cookies[i];
        var eqPos = cookie.indexOf("=");
        var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
        document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
    }
}

function clearAndRedirect(link) {
    deleteAllCookies();
    document.location = link;
}


<a href="javascript:clearAndRedirect('start.html')">Start your bacon</a>






另一种方式:

如果您为锚标记设置唯一ID或类,也可以这样做,

if you set a unique id or a class for your anchor tag, also you can do that like this way,

<a href="#" class="startbacon">Start your bacon</a>

$(document).ready(function() {
    $(".startbacon").click(function() {
        deleteAllCookies();
        document.location = "start.html";
    });
});

这篇关于用于清除cookie并重定向到html页面的Html按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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