John Doe
John Doe

Reputation: 2060

Popup Window Setting Focus

I have a popup window called myWindow and when it pops up i want the focus to be on the original window, not the popup. I took out the line myWindow.focus();and it still focused on the popup. Does anyone know how to keep the focus on the original window when the popup is created? Live Long and Prosper.

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function openWin()
{
myWindow=window.open('','','width=200,height=100');
myWindow.document.write("<p>This is 'myWindow'</p>");
myWindow.focus();
}
</script>
</head>
<body>

<input type="button" value="Open window" onclick="openWin()" />

</body>
</html>

Upvotes: 4

Views: 15757

Answers (4)

RandallB
RandallB

Reputation: 5575

In modern browsers it seems impossible to focus the original window.

Upvotes: 0

Sarjith Pullithodi
Sarjith Pullithodi

Reputation: 89

I was facing the same issue. I found a workaround.

I modified like this:

myWindow.document.write("<p>This is 'myWindow'</p>");
myWindow.document.write("<script>window.opener.focus();</script>");

It worked for me in FF and Chrome.

Upvotes: 1

jeff
jeff

Reputation: 8348

Just call window.focus() immediately after opening the popup, either in the function or right after it:

<input type="button" value="Open window" onclick="openWin(); window.focus();" />

or

function openWin()
{
myWindow=window.open('','','width=200,height=100');
myWindow.document.write("<p>This is 'myWindow'</p>");
window.focus();
}

Upvotes: 3

Elliot Bonneville
Elliot Bonneville

Reputation: 53351

Call window.focus() instead of myWindow.focus().


If that's not working, try blurring the window and then re-focusing it:

window.blur();
window.focus();

Upvotes: 4

Related Questions