5
window.onload = function() {
window.onfocus = alert('example');
}

I've met this problem, anyone can help? I'm new at javascript and made this expecting to work properly, but it does not :)

I want to alert the word "example" when the page is fully loaded and active, but don't want to alert the word "example" if the page is fully loaded but not active (onblur). And when user comes back (onfocus) then alert "example".

Konerak
  • 39,272
  • 12
  • 98
  • 118
Henrikh
  • 150
  • 2
  • 3
  • 11
  • Many of these answers seem to work for me, but none seem to work for you. Try this: `window.onload = function() {alert("Load event"); };` and `window.onfocus = function() { alert("Focus event"); };` These won't solve your problem, but they should give you an idea of when the events are happening on your page. Each of these answers is based on the assumption that, when you open the page, the "load" event happens first, and the the "focus" (if applicable). Also, the "focus" event should happen whenever the window loses and then regains focus. Is this what happens? – Ord Jun 21 '11 at 17:17

4 Answers4

7

Your code calls the alert function immediately and assigns its return value to onfocus.

You need to set onfocus to an anonymous function that calls alert:

window.onload = function() { 
    window.onfocus = function() { alert('example'); };
};
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • when I go to the link alert doesn't come. when I click someonewhere on the page, then comes. I need automatically, without any click. – Henrikh Jun 19 '11 at 15:54
  • Then you want to call `alert` immediately, not in `onfocus`. – SLaks Jun 19 '11 at 15:55
  • yes, you are right, but I want to after page fully loads and when page is active. I want not to alert, if the page is unloaded or is inactive. – Henrikh Jun 19 '11 at 15:56
2

Try this:

var hasFocus=false;
var loaded = false;

window.onload = function() {
    if (hasFocus) alert('example');
    loaded = true;
};
window.onfocus = function() { 
    if (loaded) alert('example');
    hasFocus = true;
};
window.onblur = function() { hasFocus = false; };
Ord
  • 5,693
  • 5
  • 28
  • 42
  • Thank You very much for helping. And thanks for giving time to this. But please note, when I put this code, it again calls alert, when the page is opened first time and is inactive. I go the link and change window to inactive (onblur) very quick that it don't reach to fully load and alert comes. I don't want it. :) – Henrikh Jun 19 '11 at 16:04
  • Hmmm.. When I run this code, this is the behaviour I get: When the page is done loading, it pops up an alert _if the page has focus_. If I switch focus quickly before the page is done loading, I get no alert message. As well, after the page has loaded, whenever it loses focus and then gets focus again, a message pops up. Is this not the expected behaviour? – Ord Jun 19 '11 at 23:44
  • PS - I edited this code shortly after posting it. Are you sure you are running the most up-to-date version? – Ord Jun 19 '11 at 23:46
  • The behaviour You explain is what I want, but the script is written now in Your answers doesn't do it... I tried with chrome and firefox both. – Henrikh Jun 20 '11 at 16:37
  • PS - I never saw any alert, whatever I did. – Henrikh Jun 20 '11 at 16:37
0
window.onload = function() { 
    window.onfocus = function() {
        alert('example'); 
    }
}
Connor Smith
  • 1,274
  • 7
  • 11
  • when I go to the link alert doesn't come. when I click someonewhere on the page, then comes. I need automatically, without any click. – Henrikh Jun 19 '11 at 15:54
  • You said nothing about a link, you said you want something to come up when the window is on focus. – Connor Smith Jun 19 '11 at 17:50
  • this will be in a page yes? I mean the same page's link... I need a script that will star a funtction() automatically when page load finishes and use is watching on that page, and don't start that function if user don't look at that page. – Henrikh Jun 19 '11 at 23:21
0

Here is the javascript that you need.

<html>
<head>
    <script type="text/javascript">
        window.onload = init;

        function init() { window.onfocus = whenInFocus; window.onblur = function() {window.onfocus = whenInFocus;};};
        function whenInFocus() {alert('example'); window.onfocus = null;};
    </script>

</head>
<body>
    hello
</body>
</html>
6nagi9
  • 534
  • 2
  • 7
  • 23
  • This doesn't start alert automatically and doesn't start even when I go from tab and come back, never starts – Henrikh Jun 20 '11 at 16:32
  • Are you sure it doesn't work? I have edited it to include the HTML, you may try it again. Moreover what I recently found out is that the order of execution of events varies among different browsers. So the first window.onfocus may fire before or after window.onload or may not fire at all. You would be better of using jquery for your purpose. – 6nagi9 Jun 21 '11 at 18:32