I have a div that toggles in and out of display when you click on another div. How could I modify my code so that when the user minimizes the whole browser window it automatically toggles, hiding the div from view so when the user un-minimizes window the div is no longer visible.
Minimizing the window (or switching to another application) should fire the window.onblur event. Activating the window should fire window.onfocus
Implementation details may differ slightly between browsers but there seems to be no better way in default javascript (may be you can use some flash object if flash can detect minimizing/maximizing of the window to fire necessary events, but I'm not familiar with flash)
From a quick search it looks like you can attach an event to the windows resize event, then call the required toggle functions from there as usual. I havnt actually tested the linked sample though...
$(window).bind('resize', function() { ....
http://snipplr.com/view/6284/jquery--window-on-resize-event/
In JavaScript, there is no way to detect when a window is minimized.
You could try detecting a resize or blur event on the window, however those can be triggered by things other than a minimize.
Related
Is there a Javascript event that I can hook into that fires when the page is refreshed, and Safari 'jumps' back to the scroll position you were at?
It's very frustrating, as the scroll event only fires on user/touch-induced scrolls, so will not fire in this case. I need to specifically find a way to bind to that event, as DOMContentLoaded, for example, fires even before that, and the window's load event would fire too late, as that will wait for all content to load.
Reason for this is that I am checking if an element is in view (using getBoundingClientRect).
Am I missing something here? As I'm not using jQuery, but vanilla JS, I have no document.ready() to try (though judging by the source code of it, I doubt it would work).
After some experimenting, it turns out that the load /onload event on the window triggers this jump in Safari Mobile (and presumably other browsers too), so binding to that event would suffice.
I hope this helps someone!
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.
Is there any DOM event for when the browser tab loses/gains focus? I know there are the blur and focus events on window, but they also fire when the browser window as a whole loses focus. The browser might then be still visible to the user. Of course such an event would be browser specific, but that's ok.
The reason why I want this is because I run animations that might consume quite some CPU time. When the browser tab is not visible there is no reason to continue animating. Now I know that modern browsers reduce the timer resolution of background tabs, but I could actually pause the animation, so that no CPU time whatsoever is consumed.
In case you are wondering, this is what I'm writing:
http://panzi.github.com/Browser-Ponies/
At least Google Chrome supports a webkitvisibilitychange event and a document.webkitHidden property. See the visibility API. But it seems only to fire when the shown tab changes, not when the whole window is minimized. There also seems to be a visibilitychange event for Internet Explorer, but the documentation doesn't say anything about it.
The closest thing I believe you'll find is the top answer here:
Is there a way track the focus on tab with Javascript?
Now they have exactly what was needed:
https://developer.mozilla.org/en-US/docs/Web/Guide/User_experience/Using_the_Page_Visibility_API
My Xul app will run in a very slow system, and the "active" event of the Xul windows is called while my window is still not visible (the window area is black waiting for the window to be painted). A second later the window is painted.
Is there an event that tells when the window is visible (painted, not black)?
Maybe the MozAfterPaint event will help you.
Does window.onload work any better?
I think you are dealing with the limitations of old hardware :)
If there is an image in your page, you could try adding onload to the image and see what happens...
If you are using a specific box, couldn't you use window.setTimeout()?
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.