I'm writing a turn-based game in which the server may send messages to the javascript client at random times. When the client receives these messages, it puts an asterisk at the document's title. My problem is, how do I detect that the user is viewing the page, so that I can remove the asterisk as soon as the user has seen the new messages?
For instance, how does Gmail and Facebook do it? I heard that one cannot put an onfocus property on the body object.
I would also like to know why the body doens't have the onfocus property.
You can check document.hidden, or use document.visibilityState for more options.
Support is pretty good.
Why not listen for the focus and blur events on window?
window.addEventListener('focus', onFocus); // window has focus
window.addEventListener('blur', onBlur); // window lost focus
Related
When ever u made some change to page and try to leave chrome/firefox throws a alert. Data you entered may not be saved with (Leave or Stay) buttons.
How i can do certain action when user clicks leaves or stay.
To which event i need to listen to to make actions when we click leave or stay?
TIA
There is no window close event.You can use onbeforeunload to prevent asking for simple confirmation etc.
That is what is done internally by website developers on their page. But you can't read that answer from an user script. It is simply the website developers implementation.You can't read user's response given to website developers confirm question by user from an application.
Those popups are implemented by the corresponding websites not browser.
Solution
Modern browsers now consider displaying a custom message to be a security hazard and it is removed therefore. Browsers now only display generic messages. So we can simply do this:-
window.onbeforeunload = function() {
return false;
};
window.onbeforeunload = null; // this will not show any popup.
So just remove everything fron onbeforeunload and return false and make it null. This will solve your problem.
Note: You can't show custom message or do anything significant using onbeforeunload. That is the answer to your question originally asked.
window.onbeforeunload = goodbye;
I currently use the code above to detect the onbeforeunload event, but I'm wondering if there is a way to use the event object that was passed in to determine if the event was fired due to the back button being pressed or if it was from the user attempting to reload the page.
There is no life or death scenario here as Chrome (possibly others) tailors the text of the button.
I'd simply like to the tailor the message that I display...and it's bugging me that I can't find it when looking through the object in Chrome.
Or more specifically - how (or actually - can you) detect if the current window has focus (i.e. it is the active window) when the window just opens?
I know I can listen for window.onblur and window.onfocus, but I'm trying to figure out how to address users that "open link in background tab/window" and the code starts running without either the onblur or onfocus events being called.
Unfortunately, You cannot detect if window has focus in Javascript. You can only notice when it get or lost focus using onfocus and onblur, as You said.
Some Flash video players start playing when the window receives focus. So, it seems that there is at least a way to do this in Flash (I'm no expert!). If there's no pure JavaScript way of achieving this (I can't think of any hacks at the moment), you could embed an invisible Flash applet that notifies your JavaScript code when the window receives focus.
This question already has answers here:
Identifying Between Refresh And Close Browser Actions
(13 answers)
Closed 6 years ago.
I am currently looking at the "unload" event of a window to try to determine how the "unload" event was triggered, but am having little success. Is there a way to determine how the javascript event was triggered?
Page Refresh
Back Button (or navigate away from the page)
Closing the Browser
Essentially I need to execute some code only when the browser window is being closed, not refreshed or navigated away from.
Purpose: When a customer does an update of our software, the update will redirect their first Internet request to an offer page. There is a button for a "Do Not Bother" option, but some users will simply close their browser. Upon closing the browser, I need to duplicate the "Do Not Bother" functionality so the user no longer gets redirected to the offer page. Simply attaching to the "unload" event will not work due to the different ways of leaving a page.
No, and if there was it would be browser dependent.
What kind of code are you trying to run when the user closes the page?
Is it to logout the user?
Then the user would not be logged out if the browser crashes or the network connection breaks (and probably not if the computer goes to sleep/hibernation mode).
If it is for logout-purposes you should probably use a timestamp variable at the server that gets updated with every request (or use a ajax-ping), and logout the user if it hasn't been seen for a specified time.
Update: Found this answer here at stackoverflow.
Yes, there is a solution!
I've designed a solution based on onBeforeUnload+onLoad events, HTML5 local storage and client/server communication. See the details on https://stackoverflow.com/a/13916847/698168.
I use a method of doing keyboard "sniffing", in that it looks for keydown's of "F5", "ctrl+r", "alt-f4", "backspace" and others, and if it finds them flowing through the keyboard event queue, it sets boolean variables appropriately to trap that status... then I use a "onbeforeunload" function handler, which tests against those boolean status variables to decide what to do.
You can even shut down various keyboard strokes (like "ctrl+n" or "F1" for instance) by using preventDefault(), bubbles=false and returnValue=false in your keyboard handling.
This stuff is not for the faint of heart, but its certainly doable with some persistence and lots of cross browser testing!
I am writing an ASP.NET application that tracks the user's scores and info (it is a
training application) to an access database, if any one closes the browser directly I want to display an alert message.
My problem is that I cannot use the unload event because When I pressed any ASP.NET button at that time unload event occurs. What event should I be using that will work for IE and FF?
I want to handle event Browser close(X).
The onbeforeunload event will only give you the confirm box with a message. You can't put more functionality into it.
What you should be doing is having both the window unload eventhandler and the logout button click eventhandler call the same logout method. You don't need to stop the user and ask them to press your button, your button should be just another way of doing the same thing.
Also: have you considered SCORM?
Not sure you know what you want to do here. ;-)
Do you want to prevent the user completely from closing the browser until a certain point in the trainig? If so, have you considered using a modal and maximized window? You should be able to maximize the window without the titlebar, buttons and menus.
Or if you just want to keep track of the progress, scores or similar, then you could use cookies handled by the browser via JavaScript. Just bear in mind that this will keep the data on just one machine for the student.
If you just want to warn the user that he is about to leave the training course, you could use onbeforeunload
I think others have adequately answered your specific question, but Tooney raises some good points. To expand on these. Where are you currently maintaining your the state? Are the scores stored in cookies, within a server-side session or do your persist them page by page within the database.
Assuming this isn't a cheap and cheerful solution, then I would suggest you consider persisting results page by page, as it is minimise the loss of information caused by a premature exit (either by design or accident). Of course, you then need a process to cleanup incomplete training session.
You could still use the onbeforeunload function to trap user exits, but personally I don't like UI's that double check users actions.
Good luck.