Onfocus event does not work always

Need technical advice on coding your web pages? Covers HTML, JavaScript, CSS, and some server side technologies. Also the issue of some webpages not displayed well.

Moderator: Don_HH2K

Onfocus event does not work always

Postby Gendryn » Thu 16 Jun, 2005 9:16 am

Hello,

I have a strange problem and hope that somebody can help me.

Description:
There are two pages: main.jsp and popup.jsp

Main.jsp contains a button. When you click the button, it calls a popup (popup.jsp) by using the window.open method.

I want to ensure that you can not do anything on the main.jsp as long you do not close the popup.

Therefore I use this JavaScriopt code in the main.jsp:

Code: Select all
window.onfocus = closePopup;

function closePopup()
{
if (popup!= null && popup.closed == false)
{
shallThePopUpBeClosed = confirm('Do you really want to close the popup?');
if (shallThePopUpBeClosed == true)
{
popup.close();
}
else
{
popup.focus();
}
}
}

// This is called when you click on the button to open the popup.
function openPopUp()
{
popup = window.open('popup.jsp', 'PopupWindow', 'height=350, width=800, scrollbars=no, toolbar=no, resizable=no,left=150, top=200');
}


This works in 90% of all tests.
But sometimes it does not work... the onfocus event seems to be ignored (I tested it with an alert('test')in the function closePopup() and in those 10% of the tests it is not called).
The strange thing is, that it seems to be a random problem... it appears sometimes after the 1st try... sometimnes after then 20th try. It is really random.

What may be the reson?
A bug in Internet Explorer?
A timing problem of the event onfocus?

Thanks for any hint!

Miroslav

P.S.:
I am using Windows 2000 SP4 and Microsoft Internet Explorer 6.0 SP1.
UserAgent: Opera/8.0 (Windows NT 5.0; U; de)
Gendryn
new member
new member
 
Posts: 3
Joined: Fri 13 May, 2005 7:15 am

Postby akbash » Thu 16 Jun, 2005 8:20 pm

Me, I don't know much about IE bugs. If you suspect one, your best venue for finding details is probably an IE support forum. A good place to start is the Knowledge Base at Microsoft IE Support. I searched for "onfocus" and noticed nothing interesting. You may have more luck with more search imagination.

Stylistically, your scheme sounds workable. Remember that window focus is tricky when mixed with alerts and prompt windows. Your confirmation dialog affects window focus all by itself, as do the alerts you're using to verify window focus. I suspect that the actual cause of the problem you're seeing is less an IE bug, more a need to handle focus and blur events more defensively in your script.

The first step in debugging something like this is to set up a focus/blur indicator completely contained within the windows you already have open. Alerts only stir the pot. With a cleaner indicator, I would look for the occasional focus event dropped on the floor because it arrives at an unexpected time.

One thing more. Obviously if it is an IE bug, a workaround is more useful to you than a commemorative plaque. Have you tried using the blur event on your secondary window? Sure, that's not quite the same thing, but it may be useful.
UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8b2) Gecko/20050614 Firefox/1.0+
akbash
silver member
silver member
 
Posts: 364
Joined: Mon 09 Feb, 2004 9:13 pm

Postby Gendryn » Fri 17 Jun, 2005 3:16 am

Hi akbash,


thanks for your reply.
I also thought that the confirm window may be the reason for the problem. Therefore I deactivated it... and the problem randomly still appears.
So it is not the reason.

Thanks for the idea to use the blur event on the pop up window!!!
I will try it... perhaps it works better than the onfocus event.

Bye,
Miroslav
UserAgent: Opera/8.0 (Windows NT 5.0; U; de)
Gendryn
new member
new member
 
Posts: 3
Joined: Fri 13 May, 2005 7:15 am


Return to Web Design and Page Coding

Who is online

Registered users: Bing [Bot], Google [Bot]