Last active
August 29, 2015 14:24
-
-
Save duzun/c38593ca00c27bde4a3f to your computer and use it in GitHub Desktop.
Logout from a website using HTTP Authorization - Bookmarklet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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