views:

605

answers:

3

I've got a simple dropdown menu that I want to hide whenever you click anywhere on the page (not on the menu). This works fine in FF and IE until you add iFrames into the page. The menu doesn't hide in IE when you click on the iFrame. Works fine in FireFox.

I tried using document.onclick and window.onclick.

Any ideas?

Edit: I would prefer not to have to add anything to the iframe. The page is dynamic, and different iframes could be loaded after the menu has already been created. It would be a hassle/undesirable to have to constantly watch for new iFrames and attach events to them. Yes I am aware of jQuery.live, but we don't use jQuery.

I assume this behaviour is possible since it works on FireFox, I just feel as though I may just be attaching the listener to the wrong event type or the wrong element.

+1  A: 

On the parent page, you can search for iFrames in the page and add an onfocus event for them. That event will be fired when the user clicks within the frame.

Justin Ethier
I would prefer not to have to add anything to the iframe. See edit of question.
Chris MacDonald
No, I meant trying to add this event to the iframe element in the DOM of the parent page.
Justin Ethier
I decided to search for iFrames in the page whenever the menu was shown, and add a focus event listener to them. Thanks!
Chris MacDonald
Glad to help. I will update my answer accordingly.
Justin Ethier
Does this work in all browsers?
Tim Down
Yes, it should. Are you having problems with a specific browser/version?
Justin Ethier
+1  A: 

An alternative would be to have the drop-down menu disappear after a set period of time has elapsed since the mouse or focus was on it rather than requiring a click to dismiss it.

Tim Down
Good idea, but still not the functionality I want. Thanks, might end up being a workaround.
Chris MacDonald
+1  A: 

click events bubble up to the owner window and no further. If you want the parent window to find out about clicks on the content inside another frame, you must catch events on its window/document (or have the child document catch clicks and inform its parent document). Yes, it will be a hassle, and jQuery live wouldn't work anyway since it relies on event bubbling.

Alternative approach: when you open a dropdown, also open a transparent ‘shade’ div behind it (but in front of everything else on the page including the iframes), and catch clicks on the shade to close the dropdown.

bobince
That does have the disadvantage of disabling other UI elements in the page for that click.
Tim Down
I meant using something like $('iframe').live("click", handler) to attack an onclick handler to all future ones.Another good idea, but then if the menu was up I would lose being able to activate other links/buttons on the page in one click. The first click would be on the shim and it would take another click to hit the actual link/button. Users might be confused why they (sometimes) have to click twice on a link.
Chris MacDonald
That's not necessarily a bad thing, though: browser widgets like the drop-down in a `select` box already have exactly this behaviour.
bobince