Anda dapat melakukannya seluruhnya dalam JavaScript:
IE memiliki (untuk waktu yang lama) API standar untuk membersihkan cache Otentikasi Dasar:
document.execCommand("ClearAuthenticationCache")
Harus mengembalikan true ketika berfungsi. Mengembalikan salah, tidak terdefinisi atau meledak di browser lain.
Peramban baru (per Des 2012: Chrome, FireFox, Safari) memiliki perilaku "ajaib". Jika mereka melihat permintaan autentik dasar yang berhasil dengan sembarang nama pengguna palsu lainnya (katakanlah logout) mereka menghapus cache kredensial dan mungkin menyetelnya untuk nama pengguna palsu baru itu, yang perlu Anda pastikan bukan nama pengguna yang valid untuk melihat konten.
Contoh dasar dari itu adalah:
var p = window.location.protocol + '//'
// current location must return 200 OK for this GET
window.location = window.location.href.replace(p, p + 'logout:password@')
Cara "asinkron" untuk melakukan hal di atas adalah dengan melakukan panggilan AJAX menggunakan logoutnama pengguna. Contoh:
(function(safeLocation){
var outcome, u, m = "You should be logged out now.";
// IE has a simple solution for it - API:
try { outcome = document.execCommand("ClearAuthenticationCache") }catch(e){}
// Other browsers need a larger solution - AJAX call with special user name - 'logout'.
if (!outcome) {
// Let's create an xmlhttp object
outcome = (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", safeLocation || location.href, true, "logout", (new Date()).getTime().toString())
x.send("")
// x.abort()
return 1 // this is **speculative** "We are done."
} else {
return
}
})(window.XMLHttpRequest ? new window.XMLHttpRequest() : ( window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : u ))
}
if (!outcome) {
m = "Your browser is too old or too weird to support log out functionality. Close all windows and restart the browser."
}
alert(m)
// return !!outcome
})(/*if present URI does not return 200 OK for GET, set some other 200 OK location here*/)
Anda juga dapat menjadikannya bookmarklet:
javascript:(function(c){var a,b="You should be logged out now.";try{a=document.execCommand("ClearAuthenticationCache")}catch(d){}a||((a=window.XMLHttpRequest?new window.XMLHttpRequest:window.ActiveXObject?new ActiveXObject("Microsoft.XMLHTTP"):void 0)?(a.open("HEAD",c||location.href,!0,"logout",(new Date).getTime().toString()),a.send(""),a=1):a=void 0);a||(b="Your browser is too old or too weird to support log out functionality. Close all windows and restart the browser.");alert(b)})(/*pass safeLocation here if you need*/);