I am using this code for logging out user when closes the page, but the user will logout also when clicking on other links (same website):
$( window ).unlo
This question has been asked several times :
javascript detect browser close tab/close browser
How to detect a window close event using javascript or php
Automatic logout if the tab is closed
and you can find many others.
The short answer is : you can't. As the other answers say : there are actually many other legitimate cases where the user definitely doesn't want to be logged out when closing a tab (same site opened in two tabs, bookmark clicking, url manually modified in the navigation bar ...).
The only way I know of detecting if a user wants to log out is to put a "Logout" button on your page.
Otherwise, you should manage a timeout mechanism for your sesions (e.g. : on the server side, delete sessions after say 30 minutes of inactivity).