Using JavaScript, how can I force my user to log out before going to a new domain?
I have a web site (written in PHP) that my users need to log into, and I want to be sure that they log out when they're done. So, I have a JavaScript method that deletes the PHP session cookies, and I want to call that method (a) right before the user either closes the browser window (i.e., the DOM Window object) or (b) points their browser to a site outside of my domain. Part (a) is pretty simple using the Window.onclose
method, but I can't figure out how to do part (b). The problem is that I can't get any of the Window events to distinguish between when the user is leaving my开发者_运维问答 domain and when he's going to a different page in my domain. I've seen this on some web sites (like, banking web site for example) but does anyone know how I'd actually implement it?
Also, this is approximately the code from that blog post, but written with jQuery (which is what I'm using):
var stay_in_site = false;
$('a').live('click', function() {
stay_in_site = true;
}
window.onunload = function() {
if(stay_in_site) {
return;
}
alert("I see you are leaving the site.");
}
This blog post is about just that.
If you're using prototype, the code will look something like this. (taken from blog post)
staying_in_site = false; Event.observe(document.body, 'click', function(event) { if (Event.element(event).tagName == 'A') { staying_in_site = true; } }); window.onunload = popup; function popup() { if(staying_in_site) { return; } alert('I see you are leaving the site'); }
精彩评论