views:

386

answers:

4

I would like an event handler to fire whenever someone clicks anywhere on a window (so I can clear away a menu if they click someplace other than the menu). The following works in Safari:

   function checkwho(ev) {
      obj=(window.external) ? event.srcElement : ev.target;
      var type=obj.nodeName;
      if ((type == 'BODY') || (type == 'HTML') || (type == 'DIV')) clearmenus(); 
   }

   self.onclick=checkwho;

But it does not work in Firefox or Internet Explorer 6, i.e. the handler does not get invoked. How to make this work in Firefox and Internet Explorer?

+1  A: 

For what it may help, the following works in IE7+, Safari, Firefox and Chrome:

<body onclick="clearmenus();">

... or "programmatically":

document.getElementsByTagName("body")[0].onclick = function(){ clearmenus(); };
Daniel Vassallo
What about clicks on the menu itself?
Crescent Fresh
The `clearmenus()` function will get invoked as well. There must be some logic in `clearmenus()` to not clear the menus if the click was on top of a menu.
Daniel Vassallo
As fsb suggested, to avoid these complications, a JavaScript library like jQuery would simplify everything.
Daniel Vassallo
The problem with this is that this only works if you click on the body, which may only cover *part* of the browser window. I'd like the clearmenu function to get invoked if you click *anywhere* in the window (other than the menu itself).
Mark Klein
@Mark: If the body is 100% wide, doesn't it cover the full window?
Daniel Vassallo
no - the body in that case covers the full horizontal extent of the window, but not the full vertical extent, i.e. the body could end partway down the window.
Mark Klein
@Mark: Yes, you're right. The body might end half way into the window.
Daniel Vassallo
A: 

A better option for hiding a menu like that would be to use the onblur event.

<div id="menu-div" onclick="showMenu('menu');" onblur="hideMenu('menu');">
    <ul id="menu">
        <li>Thing1</li>
    </ul>
</div>

Javascript:

function showMenu(id)
{
    var me = document.getElementById(id);
    me.style.display = 'block';
}

function hideMenu(id)
{
    var me = document.getElementById(id);
    me.style.display = 'none';
}
Jeff Rupert
+1  A: 

jQuery makes this kind of problem very easy to solve in a cross-browser manner.

Bind an onclick event handler to body that hides the menu and another to the menu element that stops propagation of the event.

fsb
To illustrate, you'd need a `$('*').live ('click', clearmenus)` and a `$('#menu').live ('click', function (e) { e.stopPropogation(); } )`
K Prime
So quick to point to jQuery, there's so many easy ways to make this happen without having deviate from real man's JavaScript.
drlouie - louierd
A: 

I wrote a script some time ago to Determine if any "Outside" Element was Clicked with JavaScript.

Josh Stodola
Perfect - the Firefox fix someone made on your blog is what I needed - thanks.
Mark Klein
Then you should up-vote and accept my answer, please
Josh Stodola