Use MochiKit javascript to close a DIV if you click anywhere outside of the box
OK, I probably did this pretty clumsy-like but I there are some lightbox javascript things out there that piss me off because I have to explicitly click a "close" button to eliminate the lightbox layer. Until now, I haven't been able to think of a good way to close the layer simply by clicking outside of the box.
It seems like a simple problem if you use some combination of mouseover and mouseout or but for some reason Firefox wasn't liking my mouseouts. I'm not trying to mimic a hover like most navigation menus do either. Here is specifically what I hoped to accomplish:
- click on a button to open an absolutely positioned popup layer that holds an AJAX search form
- I can move the mouse anywhere within the layer and click anywhere within that box without closing the layer.
- If I click anywhere on the page OUTSIDE of the popup layer it should close.
- Accomplish this with MochiKit, no pulling stuff from other js libraries.
The link opens a search:
<a onclick="javascript:showSearch(event,'otherbox')">Click me!</a>
This is my theoretical box:
<div id="otherbox">I'm an absolute position DIV layer. should show up wherever. yay!</div>
This is my javascript:
function showSearch(e,id)
{
// fade in the 'otherbox' layer
// take advantage of the afterFinish effect option
// is necessary because cannot calculate the
// dimensions of 'otherbox' unless it's in the DOM
// i have to use the function () {} in afterfinish
// because mochikit expects that to be a function.
appear(id,{duration:0.2, afterFinish:function () {delayedWindowCloser(id);} });
}
delayedWindowCloser = function (id) {
//scrub is a custom defined object of my own type
var scrub = {
'id':id,
'pos':getElementPosition(id) ,
'dim':getElementDimensions(id) ,
'calc': function(evt) {
//evt is passed in automagically by browser
var click = evt.mouse().page;
//find the bounds of the box
if (
(click.x < this.pos.x) ||
(click.x > this.pos.x + this.dim.w) ||
(click.y < this.pos.y) ||
(click.y > this.pos.y + this.dim.h)
)
{
//to get here i must have clicked outside the box boundary
//thus, close the box.
fade(this.id,{duration:0.2});
}
}
};
//make an 'onclick' event for the browser window.
//if i click anywhere in the browser it will fire the
// 'calc' function defined in my 'scrub' object
// that i just defined above
connect(window,'onclick', scrub , 'calc' );
}
Nuts!
My ISP crashed and consequently went out of business. I have moved my blog. For new articles visit blog.infoentropy.com
tags in Articles
User login
Other blogs
Recent blog posts
- Step-by-step to build a facebook app with CakePHP
- Enabling CakePHP 1.1 on Lighttpd 1.5
- Install and configure PHP5 and lighttpd on mac os x 10.4
- lighttpd as a startup item on os x
- Mac tips for Unix users
- Moving your cursor in a terminal with bash shell (Mac OS X, and Ubuntu Linux compatible)
- linux user management commands
- Setup LigHTTPD virtual hosting by hand
- Reload your hosts file in Mac
- Using MochiKit doXHR

imperfect for forms
This method has some faults with forms if you start a click in a text input box and highlight text but finish your click outside of the DIV. It causes the DIV to close presumably because when you release your mouse-click the calculation thinks you clicked outside the DIV box.
Apparently the mouse release is where the "onclick" event typically originates. After fiddling around with it you can figure out the start location and end location using "onmousedown" and "onmouseup" respectively but its a goddamn pain in the ass so I skipped using this method of closing out the DIV for the case I was hoping to resolve.
However, for standard DIV closing I think it still works like a charm (not tested for bullet-proofing)