Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

window.open, error handling and a code rash.

Status
Not open for further replies.

cyclegeek

Programmer
Sep 4, 2001
62
US
Hello everybody!
(hello, Dr. Nick)

Seriously - I have this site written in Flash and PHP and one part of it has to pop up in its own window (Gaa - not a popup?!?) - yeah, a popup. But the client gets what the client wants. So Microsoft has this bit of Javascript that's supposed to demonstrate how to pop a window and see if there are any errors caused by popup blockers - EXCEPT, the error handling doesn't seem to handle. Bend your orbs around some of this code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>Popup Tester</title>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<P><INPUT type="button" value="Open" onclick="openWin()"></P>
<P><INPUT type="button" value="ShowHelp" onclick="showPopup()"></P>
</form>
</body>

<script language=javascript >
function openWin()
{
try
{
window.open("

}
catch (e)
{

alert("Popup window blocked " + e.number);

}
}

function handlePopupErrors()
{
window.alert("Error occurred");
window.navigate(" return true;
}
function showPopup()
{
window.onerror = handlePopupErrors;
window.open("
}

</script>
</HTML>

My environment is Windows XP with SP2. When I turn the popup blocker on high the above code doesn't popup the window and doesn't catch any errors. Now, I'm not much of a Javascripter - so I'm kinda stumped. If this makes sense to you and you feel like sharing, you'll have my eternal gratitude (or at least until I get stumped with something else).

Love,
cyclegeek
 

Rather than put a try catch around the window.open, I would have thought it easier to assign the result of window.open to a variable:

Code:
var winHandle = window.open(args);

and then test for null:

Code:
if (winHandle == null) alert('Window was blocked');

Seems to be a much more logical way of doing things... Not sure why MS went down the try/catch route.

Hope this helps,
Dan
 
Hey Dan - thanks for the response. That's exactly where I want to go. I figure, window.error isn't seeing anything wrong with window.open returning null so the handlePopupErrors() function isn't even getting hit.

I changed the showPopup() function to include a variable I can test.

function showPopup()
{
window.onerror = handlePopupErrors;
var winhandle = window.open(" window.alert(winhandle);
if winhandle == null
handlePopupErrors();

}

However, now I get an error on the page saying 'object expected' on line 9 which is the input button that triggers the showPopup function. Any advice?

Cheers,
cyclegeek
 
Code:
if [red]([/red]winhandle == null[red])[/red]
handlePopupErrors();

I've found that Object Expected errors in calling from outside the function are usually caused by missing parentheses, brackets, braces, etc. or mismatched pairs. The above might fix it.

--Chessbot

"See the TURTLE of enormous girth!"
-- Stephen King, The Dark Tower series
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top