detect closing current tab on browser and call ajax function [duplicate] - javascript

This question already has answers here:
JavaScript, browsers, window close - send an AJAX request or run a script on window closing
(9 answers)
Closed 6 years ago.
I am working on a website, where at a time one admin can login. I implemented this by saving value in database. Now the problem is when current admin forget to logout, the value in database do not change and another admin can't login again. Even this current admin can't login later on, cause the value in database is checked in condition.
I have looked in window.onbeforeload and window.onunload functions, but it only trigger on page refresh etc, not detecting browser close tab/window. I want to detect closing browser tab and call function upon close, so I can change value in database using ajax.
Any help will be appreciated! Thanks

The event you are looking for is onbeforeunload

Use window.uneforeunload which is trigged when tab/window is closed.
Updated
window.onbeforeunload = function (){
// update your database from here before page closed
}
DEMO

Related

Trigger an event only on page close [duplicate]

This question already has an answer here:
javascript beforeunload detect refresh versus close
(1 answer)
Closed 1 year ago.
I am trying to trigger a function in my vue.js app only when the user closes the app. I already found the possibility to register an event listener for beforeunload. Using this event, the function will also trigger when the page is just reloaded - which is not really what I want.
Can anybody tell me if there is a way to prevent beforeunload from triggering on refresh? Or is there a completely different way to fire a function whenever my page is closed?
Thank you in advance! :)
I don't think there is a way to differentiate between a refresh and a page close, but you can maybe set a timestamp in local storage and then check that timestamp on load and if it's close you can assume it was a refresh. The before unload will still run though, but maybe you can write some js to undo whatever the before unload did when it was a refresh.
Edit: This answer to a similar question may also be helpful:
https://stackoverflow.com/a/14893469/5460296

How to find the tab and refresh it in browser by name [duplicate]

This question already has answers here:
Communication between tabs or windows
(9 answers)
Closed 5 years ago.
I have 2 open tabs from same application and same domain: tab1 and tab2, they are not opened from each other I mean I can not use win.open(....) and win.reload() So is there any way to find the tab by name and refresh it. The objective is from tab1 find the tab2 and refresh it automatically. any idea?
I see 2 solutions to your problem:
check if your document focus has changed. You only need to refresh it when tab gets visible again.
register open tabs in a session array and flag them with ajax calls or with server side code if they need to refresh. each page (tab) can check it's flag, refresh itself if needed and clear flag. I will need to know what might trigger the need to refresh for a better approach.

How to disable browser backbutton after signout from website in java [duplicate]

This question already has answers here:
Prevent user from seeing previously visited secured page after logout
(7 answers)
Closed 6 years ago.
How to disable the browser backbutton after signout / logout for my website. I am using jsp and struts1, i am using this code but this is not working properly (i.e in all pages the browser backbutton is not working but my requirement is was not that one) .
<script>
history.forward();
</script>
Please any one help me.
You don't need to disable the browser back button. You need to expire/invalidate the session when the user clicks logout. Then even if he clicks the back button, your app should detect that this request now comes with an invalid session, and so it would forward him to the login page (as he has no valid session already).

How to trigger jQuery when the browser is closed [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
javascript detect browser close tab/close browser
Does anyone know a reliable way to listen out for a window closing event in javascript/ jQuery?
The window is the parent and not any child instances. I.e. if a window is closed by mistake and the visitor launches their browser again and loads the url previously visited once more.
You can use the window.unload event to set a cookie or use local storage to save the time using new.date(), then see if the visitor returned within a set amount of time.
Something like:
$(window).unload(function() {
localStorage.setItem(“theyLeft”, new Date());
}
then on load check for :
$(window).load(function() {
var timeGoneBy = new Date() - localStorage.getItem(“theyLeft”);
//calculate time gone by, and do something if visitor returned within given time etc.
}
Would need to be refined a lot, and local storage should have cookies as fallback, but just to show the jist of it.
Try the unload method.
The unload event is sent to the window element when the user navigates
away from the page. This could mean one of many things. The user could
have clicked on a link to leave the page, or typed in a new URL in the
address bar. The forward and back buttons will trigger the event.
Closing the browser window will cause the event to be triggered. Even
a page reload will first create an unload event.
You can also try playing with the JS onunload and onbeforeunload events.

How to alert user to not close the window while he has submitted the form to process? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Javascript To Get An Alert When Closing The Browser Window
I am having a site on which users are visiting and uploading video, in that i am having a problem that users are closing the browsers or navigating from the page without letting the upload process to complete, i want to show a alert box to the user to not close the window or not to navigate from the page when uploading process is going on, i tried it through window.unload but it will not work for me because as the uploading process is going on the page will be submitted so that event will always be called, please tell me the way to show the alert box to users on window close or they navigate from the page.
Thanks in advance,
Ravinder Singh
Did you try to use:
window.onbeforeunload

Categories