0

I have a problem with Javascript window focus setting. I've written a function to open a new window with the JS method window.open() and save the return value to a variable. Now I have a link on the first page and by clicking on this link the second, with window.open() opened, window should get the focus. But this won't work. Is there any way to get this working?

My code looks like this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">

<script type="text/javascript" language="JavaScript">
var myWindow;
function checkForRefresh() {
                myWindow = window.open("test.html", "TestMain");
                myWindow.focus();
}

function switchFocus(umsWindow) {
                myWindow.focus();
}
</script>

</head>

<body onLoad="checkForRefresh()"> 

<p><a href="javascript:switchFocus();">Test Link</a></p>

</body>
</html>
shirocko
  • 13
  • 3

2 Answers2

0

I got it to work when I made the functions properly global by putting them on the window object

http://jsfiddle.net/u52xE/3/

window.switchFocus = function() {
    myWindow.focus();
}

It's not ideal to have that javascript in your HTML and global functions though, I'd recommend using jQuery to attach events

actual_kangaroo
  • 5,971
  • 2
  • 31
  • 45
  • Hi thanks for this code, but it won't work in FF24. When it opens it gets the focus, but by using the link it won't get the focus. – shirocko Apr 03 '14 at 08:02
0

This problem has been talked already, some browsers does not listen to window.focus(). Please take a look at comment #2533335.

Community
  • 1
  • 1