window.focus does not work

Our web application opens multiple windows. Typically, most of windows will have a menu/link to get back to the main window. The link is a simple java-script that gets the main window handle and then uses window.focus to bring the main window into the focus. However, this functionality does not work in Safari. I have checked if the parent window handle is correct or not and its correct. I have put check to see if focus functions exists or not and it does exists but the call to it does not work. Focus does not shift from current window to main window.
FYI, the functionality works w/o any problem in Firefox3, IE6 and IE7.
The safari version is 3.1.1.
Any ideas to get focus working?

Windows XP Pro

Posted on Aug 28, 2008 5:00 AM

Reply
3 replies

Aug 30, 2008 4:00 PM in response to vinayc

I have the same problem. I would expect that the window opened would get focus once loaded. I also expect the parent would regain focus on the close of the child. I have tried using focus() function to force focus on tags or window objects(fruitless). I am guessing focus() will work on <input> tags, thus causing bubbling bring the window into focus as well. Only a guess! I have not tried it.
Main page:
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<script language="javascript" type="text/javascript" src="kb1.js"></script>
</head>
<body>
First Anchor tabindex 1


Second Anchor tabindex 2

On the return after closing the popup window this window does not get focus unless you click
somewhere on the page.


</body>
</html>

Link to page:
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<script language="javascript" type="text/javascript" src="kb1.js"></script>
</head>

<body>
Click to close


Will not open itself, which is a good thing

Notice that this window does not get keyboard focus unless you click
somewhere other than the first anchor. The onkeydown function is not active.


</body>
</html>

Script to open window via keyboard:

<!--
/* This code is for Safari: onkeypress does not fire for nav keys*/
document.documentElement.onkeydown = function(e) {
var iKeyCode = getKeyCode(e);
if (iKeyCode != 13) {return true};
var strFeatures =
",resizable=yes,scrollbars=yes,status=yes" +
",toolbar=no,menubar=no,location=no";
window.open("SAFto.html","SAFto",strFeatures);
};

function getKeyCode(e) {
var code = null;
if (!e) var e = window.event;
if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;
//alert('Key Code: ' + code);
return code;
};

-->

Aug 30, 2008 4:29 PM in response to imanut

I have the same problem. I would expect that the window opened would get focus once loaded. I also expect the parent would regain focus on the close of the child. I have tried using focus() function to force focus on <a.> tags or window objects(fruitless). I am guessing focus() will work on <input> tags, thus causing bubbling bring the window into focus as well. Only a guess! I have not tried it. Buy the way if you could tell me how to show <tags> without using a period, I would appreciate.

Main page:
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<script language="javascript" type="text/javascript" src="kb1.js"></script>
</head>

<body>
<a. href="" tabindex="1">First Anchor tabindex 1</a.>
<br. />
<a. href="" tabindex="2">Second Anchor tabindex 2</a.>
<p.>
On the return after closing the popup window this window does not get focus unless you click
somewhere on the page.
</p.>
</body>
</html>

Link to page:
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<script language="javascript" type="text/javascript" src="kb1.js"></script>
</head>

<body>
<a. href="" tabindex="1" onclick="window.close();">Click to close</a.>
<br. />
<a. href="" tabindex="2">Will not open itself, which is a good thing</a.>
<p.>
Notice that this window does not get keyboard focus unless you click
somewhere other than the first anchor. The onkeydown function is not active.
</p.>
</body>
</html>

Script to open window via keyboard:

<!--
/* This code is for Safari: onkeypress does not fire for nav keys*/
document.documentElement.onkeydown = function(e) {
var iKeyCode = getKeyCode(e);
if (iKeyCode != 13) {return true};
var strFeatures =
",resizable=yes,scrollbars=yes,status=yes" +
",toolbar=no,menubar=no,location=no";
window.open("SAFto.html","SAFto",strFeatures);
};

function getKeyCode(e) {
var code = null;
if (!e) var e = window.event;
if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;
//alert('Key Code: ' + code);
return code;
};

-->

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

window.focus does not work

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.