Thariama
Thariama

Reputation: 50832

How can i use javascript to get a browser window from background back to the front on buttonclick?

If a user clicks on a button a new window opens where the user can do irrelevant stuff. In case the user clicks on the former page the new window gets pushed into the background and the user is not able to see it anymore. That is notmal behaviour so far.

What i need now is that the already opened window comes back to the front on a second button click of the user. Is this possible? How?

Upvotes: 0

Views: 185

Answers (2)

ElPedro
ElPedro

Reputation: 586

Check to see if the window exists. If not, create it. If it does the give it the focus. Something like the following should do the trick.

<html>
<head>
<script language="JavaScript">

var popup;
function getWindow() {
    if(popup==null) {
    popup=window.open("http://www.stackoverflow.com");
    }
else {
    popup.focus();
    }
}
</script>
</head>
<body>
<input type="button" value="Click me..." onClick="getWindow()" />
</body>
</html>

Upvotes: 1

Veger
Veger

Reputation: 37906

You need to keep the window object stored in a variable, say w. Then you can use focus() to bring it to the front again:

w.focus();

Upvotes: 3

Related Questions