Log off a user when they leave web site

Hi,

I would like to log off a user if they leave my site.

I have tried a few things such as ....

        window.onbeforeunload = function(){
             $.ajax({
                type: "POST",
                //page and method
                url: "../KeepSessionAlive.aspx/logOutUser",
                async: false,
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                    error: function (xhr, ajaxOptions, thrownError) {
                    alert(xhr.status);
                    alert(thrownError);
                }
            });
         };

However this logs off the user if they leave the web page for another web page. I only want to invoke a log off if they actually navigate to another site altogether.

Would be grateful for any help.

Thanks
LVL 1
SolugaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GaryCommented:
There is no reliable way to detect it.
And even when it does work your alerts would not fire. There is only one option for the alert and that is 'Do you want to leave this website' - Yes/No (and the yes/no part you cannot change - or is it OK/Cancel - cannot remember)
0
COBOLdinosaurCommented:
Not going to happen. The browser does not know it is leaving the site until it has already left.  

Why is it you think you ned to know when they leave the site?

Cd&
0
SolugaAuthor Commented:
Because if they leave the site and do not log off the web site then another user could potentially access the system by going back in history.
0
GaryCommented:
Then you need to set a last access time as part of the profile db table and for example if it is more than 5 mins since they last accessed a page then they need to login again
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.