SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2008
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Check if window is focused

    Hey im trying to check if a window is focused. Im trying to do something like:

    if current window is focused do this

    if not do this

    Does anyone know how to do this? Thanks.

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,075
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    var window_focus = true;

    window.onblur = function() { window_focus = false; }
    window.onfocus = function() { window_focus = true; }

    if (window_focus) {
    // do something
    } else {
    // do something else
    }


    Something like that?
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Zealot
    Join Date
    Oct 2008
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey, you got the idea but when I tried to implement it, it didnt work. Here is the code I used:

    HTML Code:
    <script language="javascript">
    var window_focus = true;
    
    window.onblur = function() { window_focus = false; }
    window.onfocus = function() { window_focus = true; }
    
    if (window_focus) {
    document.title = "FOCUSED";
    } else {
    document.title = "NOT FOCUSED";
    }
    </script>

    EDIT: Did some google searching now that I got the phrase window.blur and was able to make it work by doing this:

    HTML Code:
    <title>CHECK IF BLURRED</title>
    <script type="text/javascript">
    window.onblur = blurText;
    function blurText(){
    	document.title='BLUR'
    	window.onblur = '';
    }
    </script>
    Thanks ScallioXTX!

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,075
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    To sum up:

    HTML Code:
    <title>CHECK IF BLURRED</title>
    <script type="text/javascript">
    window.onblur = function (){
    	document.title='NOT FOCUSED'
    }
    window.onfocus = function (){
    	document.title='FOCUSED'
    }
    // too make it complete, also add onblur to document.
    // For browsers using tabs (like firefox)
    document.onblur = window.onblur;
    document.focus = window.focus;
    </script>
    That should work every time the window gets / looses focus.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    SitePoint Zealot
    Join Date
    Oct 2008
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Even better. Thanks a lot! Im still trying to do the first way, with the IF. I am triggering the msg(); from flash. What its supposed to do is if there is a new msg it triggers msg(); Javascript. Flash is doing what its supposed to and I have verified that its working. The Javascript is supposed to check if the browser has focus and if it does dont change title, but if it doesnt, change to NEW MESSAGE!. This is msg(); do you see any reasons why it doesnt work?

    HTML Code:
    function msg(){
    	var focus = true;
    	window.onblur = function() { focus = false; }
    	window.onfocus = function() { focus = true; }
    	document.onblur = window.onblur;
    	document.focus = window.focus;
    	if(focus){
    		document.title="Talk";
    	}else{
    		document.title="NEW MESSAGE";
    	}
    }

  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,075
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    The way you do it, the functions onblur and onfocus are only than attached to the window and the document after msg() is called for the first time. You should do it like this (so that onblur and onfocus are always attached to the document and the window):

    HTML Code:
    var focus = true;
    window.onblur = function() { focus = false; }
    window.onfocus = function() { focus = true; }
    document.onblur = window.onblur;
    document.focus = window.focus;
    	
    function msg(){
    	if(focus) {
    		document.title="Talk";
    	} else {
    		document.title="NEW MESSAGE";
    	}
    }
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  7. #7
    SitePoint Zealot
    Join Date
    Oct 2008
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey, that one works, when I get a new msg it changes to NEW MESSAGE but when I click on the tab with the message it doesnt go back to the default title Talk. Any ideas? Thanks for your help!

  8. #8
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,075
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    HTML Code:
    var focus = true;
    window.onblur = function() { focus = false; }
    window.onfocus = function() { focus = true; document.title="Talk"; }
    document.onblur = window.onblur;
    document.focus = window.focus;
    	
    function msg(){
    	if(focus) {
    		document.title="Talk";
    	} else {
    		document.title="NEW MESSAGE";
    	}
    }
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  9. #9
    SitePoint Zealot
    Join Date
    Oct 2008
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Works like a charm. Thank you so much!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •