views:

1219

answers:

2

I'm trying to launch a popup window from a Javascript function and ensure it has focus using the following call:

window.open(popupUrl, popupName, "...").focus();

It works in every other browser, but IE8 leaves the new window in the background with the flashing orange taskbar notification. Apparently this is a feature of IE8:

http://msdn.microsoft.com/en-us/library/ms536425%28VS.85%29.aspx

It says that I should be able to focus the window by making a focus() call originating from the new page, but that doesn't seem to work either. I've tried inserting window.focus() in script tags in the page and the body's onload but it has no effect. Is there something I'm missing about making a focus() call as the page loads, or another way to launch a popup that IE8 won't hide?

A: 

You might try this. Not sure if it will work though>

var isIE = (navigator.appName == "Microsoft Internet Explorer");
var hasFocus = true;
var active_element;

function setFocusEvents()   {
    active_element = document.activeElement;
    if (isIE) {
     document.onfocusout = function() { onWindowBlur();       }
     document.onfocusin = function()  { onWindowFocus();     }
    } else {
     window.onblur = function()   { onWindowBlur();          }
     window.onfocus = function()  { onWindowFocus();       }
    }
}

function onWindowFocus()    {
    hasFocus = true;
}

function onWindowBlur() {
    if (active_element != document.activeElement) {
     active_element = document.activeElement;
     return;
    }
    hasFocus = false;
}
Illes Peter
A: 

Yeah I can't test this on IE8 at the moment either but have a play with this document.ready method instead of the body.onload:

test1.html:

<html>
    <head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
  <script type="text/javascript"> 
   function openNewWindow()
   {
    window.open("test2.html", null, "height=200, width=200");
   }
  </script>
    </head>
    <body>                        
        <a onclick="openNewWindow()">Open</a>
   </body>
</html>

test2.html:

<html>
    <head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
  <script type="text/javascript"> 
   $(document).ready(function(){ window.focus(); });
  </script>
    </head>
    <body>
        <div id="container" style="background:blue;height:200px;width:300px">
        </div>
   </body>
</html>
Ambrosia