views:

3398

answers:

4

I am trying to bring focus to window using jquery. The window is popup initiated through a button click on the parent page. I have some ajax calls going on in the child window, so data is being updated. My issue is that if the user clicks on the parent window and hides the child behind it, i would like to bring that child window back to the forefront if there is a data update.

inside $(document).ready I wire up these events:

  $(window).blur(function(){
    WindowHasFocus =false;
}).focus(function(){
    WindowHasFocus =true;
});

Then, if data is updated, I call this function:

function FocusInput(){

 if(!WindowHasFocus){
    $(window).focus();
 }
}

This works as expected in IE8, but in FireFox(and all other browsers) the Blur event nevers seem to fire if I click the parent window. Any suggestions/ideas on how achieve this?

update:

Total facepalm moment: In FireFox: * Tools * Options… * Content tab * Advanced button next to “Enable JavaScript” * check the box named "Raise or Lower Windows"

+1  A: 

It seems like you shouldn't care to know when your window got blurred. When your data updates, your window is either not in focus, in which case you want to focus it, or it is already in focus, and focusing it again doesn't hurt you any.

levik
well, the reasoning is that the child window has an text input, and i do not want focus to be taken away from that. But in any case, $(window).focus() does not seem to do anything in FF.
Mike_G
Did you try just doing `window.focus()` without the jquery call?
levik
+1  A: 

If there is not a strong reason for having two separate windows then it would be better use "modal boxes", there are plenty of examples out there and jquery plugins to achieve that. An example of such a plugin: http://www.84bytes.com/2008/06/02/jquery-modal-dialog-boxes/

Miquel
cant use a modal, it has to be a separate window.
Mike_G
In that case try to call the focus from the parent window: url='someUrl' newWin=window.open(url,"helpWin","features"); ... newWin.window.focus()
Miquel
A: 

You're absolutely correct. In FF, it seems as though it does fire the event, but at that same time, it seems like it doesn't register the element as being focused. Therefore the blur event can never be fired. Not sure I'm even explaining that correctly... The following code says it all.

In this example, the box is hidden by default, but is displayed via the focus event listener. In IE 8, if you click the main window, it still fires blur, but in FF it doesn't:

<html>
<head>

</head>
<body>
    <div id="hiddenWin" style="width: 100px; height: 100px; background-color: Black; display: none;"></div>

    <script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"&gt;&lt;/script&gt;
    <script type="text/javascript">
     var something = 12;
     something += 4; 

     $(document).ready(function()  
      {

       $("#hiddenWin").focus(function()
        {
         $(this).show();
        }
       ).blur(function()
        {
         $(this).hide();
        }    
       )    

       $("#hiddenWin").focus();
      }
     );
    </script>

</body>
</html>

For your need, would it be feasible to setup an overlay background? Something that is a fixed position @ top:0 and left:0 which takes up the whole screen and has a z-index that is less than your popup. That way, when they click the overlay, it will steal focus and then you can hide everything...? IDK, just a suggestion. I'll keep messing around and see if I can figure it out. Good question. +1

regex
no, the idea is to let the person browse the website and only bring the child window to the front when there has been a data update.
Mike_G
found it...im an idiot:In FireFox: * Tools * Options… * Content tab * Advanced button next to “Enable JavaScript” * check the box named "Raise or Lower Windows"
Mike_G
A: 

Total facepalm moment: In FireFox:

  • Tools
  • Options…
  • Content tab
  • Advanced button next to “Enable JavaScript”
  • check the box named "Raise or Lower Windows"

This is turned off by default and must be enabled. And also, i assumed that since it didnt work in Chrome, that Safari would be the same, but you know what they say about "assuming" (it works in Safari, but not Chrome).

Mike_G