1

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

I've gotten this far:

<script type="text/javascript">

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

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

Is there any easy way of doing this?

bool.dev
  • 17,508
  • 5
  • 69
  • 93
Vic
  • 188
  • 3
  • 16

3 Answers3

7

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() 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>

ANOTHER WAY:

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";
    });
});
Community
  • 1
  • 1
Okan Kocyigit
  • 13,203
  • 18
  • 70
  • 129
0

To refresh the page just add:

href="javascript:location.reload(true)"

to your anchor tag.

ewein
  • 2,695
  • 6
  • 36
  • 54
0

Since , you want to refresh the whole page will be refreshed with your actual DOM code Lets say that your button looks like:

<a href="#" id="dostuff">Click me</a>

From your sample code I am assuming that you are using Jquery

when a user clicks the button a) all the cookies are cleared b)A new cookie is set, it works as a "flag" in order to check if your page is refreshed c)The page is refreshed d) Your code checks the "flag" if is set, clears once more the cookies and redirects. For my code I am using a jquery plugin in order to manipulate cookies easily, I will also use ocanal's function (great stuff :D )

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";
    }
}

$(document).ready(function() {
    var test_flag = $.cookie('flag');
    //get cookie
    if (test_flag == "flag_is_set") {
        //if cookie is set clear all cookies once more and redirect

        deleteAllCookies();
        document.location = "myPage.html";
    }

    $("#dostuff").click(function() {
        //if button is clicked
        //clear all cookies
        deleteAllCookies();
        //set a new cookie as flag
        $.cookie('flag':'flag_is_set');
        //refresh page

        document.location.reload();
    });

});​
TheodoreV
  • 304
  • 4
  • 17