Create a gist now

Instantly share code, notes, and snippets.

Logout from a website using HTTP Authorization - Bookmarklet
/**
* Logout from a website using HTTP Authorization.
*
* Inspired by http://stackoverflow.com/a/14329930/1242333
*
*/
(function(window,location,command,outcome,undefined){
function done(e) {
if ( !e || this.readyState == 4 ) {
outcome && location.reload();
outcome = undefined;
}
}
// IE has a simple solution for it - API:
try { if ( outcome = document.execCommand(command) ) done(); }catch(e){}
// Other browsers need a larger solution - AJAX call with special user name - 'logout'.
if (!outcome) {
// Let's create an xmlhttp object
(function(x){
if (x) {
// the reason we use "random" value for password is
// that browsers cache requests. changing
// password effectively behaves like cache-busing.
x.open("HEAD", location.href, true, command, Math.random()+'');
x.onreadystatechange = done;
x.send("");
outcome = true;
}
else {
alert("Your browser doesn't support log out functionality. Close all windows and restart the browser.");
}
}(window.XMLHttpRequest ? new window.XMLHttpRequest() : ( window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : undefined )));
}
}(this,location,"ClearAuthenticationCache"));
(function(c,d,e,b,f){function g(a){a&&4!=this.readyState||(b&&d.reload(),b=f)}try{(b=document.execCommand(e))&&g()}catch(h){}b||function(a){a?(a.open('HEAD',d.href,!0,e,Math.random()+''),a.onreadystatechange=g,a.send(''),b=!0):alert('Your browser doesn\'t support log out functionality. Close all windows and restart the browser.')}(c.XMLHttpRequest?new c.XMLHttpRequest:c.ActiveXObject?new ActiveXObject('Microsoft.XMLHTTP'):f)}(this,location,'ClearAuthenticationCache'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment