简体   繁体   中英

Giving a child window focus in IE8

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(VS.85).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?

The IE8 is not allowing this feature because of security issues

Windows Internet Explorer 8 and later. The focus method no longer brings child windows (such as those created with the open method) to the foreground. Child windows now request focus from the user, usually by flashing the title bar. To directly bring the window to the foreground, add script to the child window that calls the focus method of its window object

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

The problem is the Window.focus method does not work in Internet Explorer 8 (IE 8). It's not a pop up blocker or any settings in IE 8 or above; it's due to some security I believe to stop annoying pop-ups being brought back up to the top.

after a lot of hair pulling and googling i found the following:

Microsoft suggest updates but this doesn't appear to work plus how do they seriously expect me to ask all of the users my site to update their machines!

so I've come up with this work around or fix.

What i do with the window is:

  1. first I check if the window is open
  2. if it's open, close it
  3. open a new fresh version of the window on top.

javascript code to include at header or in separate file:

function nameoflink()
{
    var nameofwindow = window.open('pagetolinkto.htm','nameofwindow','menubar=1,resizable=1,width=350,height=250');
    if (nameofwindow) {
        nameofwindow.close();
    }
    window.open('pagetolinkto.htm','nameofwindow,'menubar=1,resizable=1,width=350,height=250');
    return false;
}

link on the page:

<a href="#" onclick="nameoflink()">Click Here to go to name of link</a>

Tested in MS Windows 7 with IE8 not sure of exact version.

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;
}

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"></script>
        <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"></script>
        <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>

I figured out what the issue was - turns out the reason running window.focus() in the onload wasn't working was because the first window.open().focus() call caused it to start flashing in the background, and after that any subsequent focus calls wouldn't work. If I don't try to focus it from the calling window but only from the popup it comes to the front normally. What an annoying "feature"...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM