I want to run some Javascript when the user clicks the Stop Load-button (red X in most browsers) or hit Esc on the keyboard, which usually does the same.
I've seen questions here covering the Esc button by hooking onto document.body.onkeyup, but couldn't find anything covering mouse click on the Stop button.
Internet Explorer has a document.onstop event that is fired, but other browsers don't seem to support that. Note that it's fired when the user clicks Stop or hits Esc, OR if the user navigates to another page during page load, which has the same effect.
I don't believe there is a reliable way to trigger an event on clicking Stop in other browsers. Perhaps it would be possible to do something like: keeping the connection to the server open (as in the Comet approach), streaming some sort of keep-alive down the connection, and detecting if the stream ends (as I assume it would if the Stop button were clicked).
If it's images that are still getting loaded on the page, you can use the onabort event to monitor for the stop load.
Monitoring for the mouse click should be impossible, as it doesn't happen inside the current browsing window.
There isn't any cross browser way of doing this.
However, IE has a special event, onstop which occurs on the body when the stop button is pressed. You cannot override the stop button functionality (that is, you cannot cancel it), but you can detect that it has happened in IE.
Related
I have a seemingly simple requirement but I have been stuck for days. Can someone give me a hand?
I need a confirmation prompt if the user tried to close the pop-up window
if the user click ok to close, I need to call an ajax call
My original design is to add an onbeforeunload event handler, have it returns a string which triggers a prompt. Works perfectly.
The problem is the next part. Added a unload listener, a pagehide listener, and a visibilitychange listener - in all three cases, Chrome doesn't fire the event if the user close the window, only if I refresh the window. Firefox works perfectly. I am using a sendbeacon call which should work in these scenarios and if I add a breakpoint to pause before the window closes, the beacon is sent, so it seems like Chrome is closing the document too fast and never bother sending the last beacon, which makes the whole exercise pointless.
Has anyone face similar issues and if so, any way to work around it?
I'm struggling with the same problem.
Reading about the event on the documentation I've noticed that it is an unstable event, and moreover in the compatibility table, Chrome is set to "not supported".
But I noticed that chrome fire the event one time only.
If I close the browser and then i re-open it, the first time the event is fired, but it not work with tab closing.
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.
on my page, I have a listener on window.onpopstate
I wish to trap the user's back arrow and run my own routine
but window.onpopstate fires when 1) the page is initially loaded, 2) is reloaded, as well as when 3) user presses the back arrow
I could create a state space flag and check it. but more natural would be to determine from the event itself what's going on.
is there anything intrinsic way to determine whether the event was fired from loading or back button? when I inspect the two events, they look pretty much the same.
The window.onpopstate event is triggered when the user navigates between two history entries for the same document, i.e. every time the active history entry changes.
It is fired when clicking on back button, on page load and also on page reload (Firefox doesn't emit this event on page load). So, the behavior you are seeing is intended.
And, for the back button, i guess there is no any way to detect the back-button-click using this event.
You can compensate for popstate bugs in browsers. When someone uses my editor and hits the Backspace button sometimes that triggers navigation (this should not be a default but not everyone is qualified for their position) so I cancel it (yes, you can) and then fix the address bar with history.go(1);. I'm not posting dependency related code here as various people will need this for different contexts but it forms the basis of what you'll need.
window.onpopstate = function(e)
{
if (id_('editor') && is_node_parent(document.activeElement,id_('editor')))
{
e.preventDefault();
history.go(1);
}
}
I know it's possible to detect right before a user closes a webpage using onbeforeunload. I was just wondering if there was a similar event fired right before the user switches tabs or minimizes. I'm not looking for an event that fires after the user changes tabs.
Is this possible?
No, there is no such event. Sorry!
I am having a problem to detect a click event outside document, for instance in the closing button of the browser without using the onbeforeunload event, because I have a JSP page that is processing something, using meta refresh to give the status of the process evolution.
Since I am using meta refresh I cannot use window.onbeforeunload event to prevent/confirm the user to exit because the meta refresh will fire up the event. Thus, I need to check manually if the mouse will be clicked outside my document.
I can check if the mouse coordinates are outside, thus canĀ“t associate an click event to that in IE8.
if (window.event.clientY < 82 && window.onclick)
Someone have any idea out achieve this issue?
Thanks in advance!
Detecting the close button isn't possible but you can detect if the user is losing focus of the browser by doing:
$(window).blur(function() {
alert('lost focus');
}
It's not possible. Events don't fire outside of the document, including clicks on the window chrome.
I think you will need to think about what you're trying to achieve. It sounds like a shaky design if you must get the close event of the page. Lots of other events will affect you if that is of a concern.
If you have a JSP page producing and showing the status by a meta refresh - what is your problem with the window closing? That should be of your concern, not how to detect a browser close event.