clickthru.js provides an abstract callback mechanism for hiding dropdowns and context menus when a user clicks elsewhere on a webpage.
First, include clickthru.js:
<script src="clickthru.js"></script>Now, declare a handler in whatever way you want:
function myHandler(e) {
console.log('the user clicked the page');
}Finally, register it:
window.clickthru.addListener(myHandler);You can deregister your listener like this:
window.clickthru.removeListener(myHandler);Instances of the ClickThruEvent data type are passed to callbacks.
ClickThruEvent objects have one static property:
- event - the raw DOM event which triggered this callback
In addition, every instance of ClickThruEvent has the following functions:
- inElement(e) - provide a DOM element and get a boolean indicating whether or not the user clicked within that element. This can be used to hide a context menu unless the user clicks within it.
- stopClickThruPropagation() - prevent the click thru event from being propagated to listeners which were added earlier. This can be used so that nested elements can be clicked away before parent elements.
clickthru.js is licensed under the BSD 2-clause license.
Copyright (c) 2015, Alexander Nichol.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.