views:

194

answers:

2

When a logged user click the signout link I'd like to call the logout action without refresh the page or redirect. Any helps? Thanks!

+1  A: 

You need to simply bind some code to your logout link/button that requests your logout action:

$("#logout").click(function() {
    $.get("/foocontroller/logout", function() {
        alert('Successfully logged out');
    });
    return false; // so the page does not refresh
});

Returning false from the click event prevents the default 'link following' behaviour which would typically cause a refresh/redirect.

karim79
Thanks for your reply. What I have to insert in the href of logout link? Thanks! :)
Your best bet is to include the same link that's being requested in the above code, so if Javascript is not available the page will 'fall-back' to visiting the link.
karim79
+1  A: 

You can even do it in a more unobtrusive way:

$('a.logout').click(function () {
    var logoutUrl = $(this).attr('href');
    $.get(logoutUrl, function () {
        alert('Logged out');
    });
    return false;
});

What this does is it simply finds the logout link ($('a.logout')) and when you click the link will get "followed" in the back, without changing the page (the $(this).attr('href') part will get the url from the link). Replace alert('Logged out'); with what you want to happen after the user has been logged out.

Emil Ivanov