Link to home
Start Free TrialLog in
Avatar of onemorecoke
onemorecoke

asked on

After window opened using window.open, how to bring it forward when the same link is clicked

I have a link on a page that is this:

<a href="javascript:void window.open('miniplayer.asp?ID=1','Mini Player','height=200,width=350,status=no,toolbar=no,menubar=no,location=no')">
<img src="/images/1.jpg" width="64" height="48" border="0">
</a>

Using Firefox, when I click on the link, the window appears.  If the window goes behind another window, if the same link is clicked, the window is reloaded (I put an alert in the onload event), but the window does not come forward.  I have tried below which does not work in Firefox, but it does using IE7:

onload="window.focus()"

  Any suggestions?


Avatar of hielo
hielo
Flag of Wallis and Futuna image

Try this:
<a href="javascript:void if(window.MiniPlayer){window.MiniPlayer.focus();}else{window.MiniPlayer = window.open('miniplayer.asp?ID=1','MiniPlayer','height=200,width=350,status=no,toolbar=no,menubar=no,location=no')}">
<img src="/images/1.jpg" width="64" height="48" border="0">
</a>

Open in new window

Avatar of onemorecoke
onemorecoke

ASKER

Thanks for the response.  I get a javascript error 'window.MiniPlayer' is nul or not an object.  What do you think?
Try this:
<script type="text/javascript"><!--
window.MiniPlayer=null;
function handleClick()
{
	if(null!==window.MiniPlayer){
		window.MiniPlayer.focus();
	}
	else
	{
		window.MiniPlayer = window.open('miniplayer.asp?ID=1','MiniPlayer','height=200,width=350,status=no,toolbar=no,menubar=no,location=no');
	}
return false;
}
//--></script>
<a href="#" onclick="return handleClick();">
<img src="/images/1.jpg" width="64" height="48" border="0">
</a>

Open in new window

Here's a better implementation. On this example the link's href value is passed to the javascript function and the function uses that to open the popup window.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<script type="text/javascript"><!--
window.MiniPlayer=null;
function handleClick(url)
{
	if(null!==window.MiniPlayer){
		window.MiniPlayer.focus();
	}
	else
	{
		window.MiniPlayer = window.open(url,'MiniPlayer','height=200,width=350,status=no,toolbar=no,menubar=no,location=no');
	}
return false;
}
//--></script>
<a href="miniplayer.asp?ID=1" onclick="return handleClick(this.href);" style="background-color:red;">
<img src="/images/1.jpg" width="64" height="48" border="0">
</a>
</body>
</html>

Open in new window

Thanks for the insight, I had to rewrite it a bit:

href="javascript:if(MiniPlayer){MiniPlayer.focus()}else{var MiniPlayer=window.open('miniplayer.asp?=1','MiniPlayer','height=200,width=350,status=no,toolbar=no,menubar=no,location=no');}"

I had to get rid of the "window." and make MiniPlayer an object before I did not get any errors.  The problem with this code though is that if the window does get close and you click on the link again, you get the error:

"The callee (server) is not available and disappeared; all connections are invalid, The call did not execute."

This would make sense because the object is no longer connected to anything.  I am trying to find out a way to see if he object is gone and reset things.  Possibly with a try/catch.
Here you go:
Notice that I got rid of 'var' in the else clause and I am also using the onclick event handler. If you use the href to execute the code, when you click on the the browser window will change
<a href="javascript:void()" onclick="if(MiniPlayer && false===MiniPlayer.closed){MiniPlayer.focus()}else{MiniPlayer=window.open('miniplayer.asp?=1','MiniPlayer','height=200,width=350,status=no,toolbar=no,menubar=no,location=no');};return false;">T</a>

Open in new window

If I remove the var, I get Miniplayer is undefined error.
Ok, I got it working.  I had to include the var or it did not work.  Also, I was able to just use what you sent and put it into the href alone.  The MiniPlayer.closed was the key.  I also change the logic to !Miniplayer.closed

Thanks for you input, it really helped.
<a href="javascript:if(MiniPlayer && !MiniPlayer.closed){MiniPlayer.focus()}else{var MiniPlayer=window.open('miniplayer.asp?ID=1%>','MiniPlayer','height=200,width=350,status=no,toolbar=no,menubar=no,location=no');}">

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok, thank you, that makes sense.  I have never seen:

false===

before.  Is that valid?

>>I have never seen: "false=== ..." before. Is that valid?
Absolutely. To clarify its usage consider this:
var x = 0;

//this will alert true because 0 evaluates to false in boolean context
alert (x==false);

//this will alert  false because false is a boolean object, but x in a Number object
//even though x evaluates to false, it is NOT the same object type as false
alert(x===false);
Thank you for your help.