Created
October 12, 2012 02:05
-
-
Save weikinhuang/3876951 to your computer and use it in GitHub Desktop.
Click capturing functionality for delaying all clicks until all events have been bound
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* preload-clicks.js v0.1.0 | |
* http://www.closedinterval.com/ | |
* | |
* Click capturing functionality for delaying all clicks until all events have been bound | |
* | |
* Copyright 2011-2012, Wei Kin Huang | |
* Classify is freely distributable under the MIT license. | |
*/ | |
var releaseClicks = (function(document) { | |
"use strict"; | |
var clicks = [], | |
// elements with the class or is in a container with the class "click-through" | |
clickthroughTest = /(?:^| )click-through(?:$| )/, bodyTest = /^body$/i, | |
// event passthrough data | |
passthoughKeys = [ "screenX", "screenY", "clientX", "clientY", "ctrlKey", "altKey", "shiftKey", "metaKey" ], | |
passthoughKeysLen = passthoughKeys.length, | |
// function to listen to click events on the document level | |
capture = function(e) { | |
var target = e.target ? e.target : e.srcElement, tmp = target, clonedEvent = {}, i; | |
// stop when we get to the body | |
while (!bodyTest.test(tmp.nodeName)) { | |
// if click through was allowed, the don't bother continuing | |
if (clickthroughTest.test(tmp.className)) { | |
return; | |
} | |
tmp = tmp.parentNode; | |
} | |
if (e.preventDefault) { | |
// Handle the general browser event prevention | |
e.preventDefault(); | |
} else { | |
// for handling IE | |
e.returnValue = false; | |
} | |
// clone event values | |
for (i = 0; i < passthoughKeysLen; i++) { | |
if (e[passthoughKeys[i]] != null) { | |
clonedEvent[passthoughKeys[i]] = e[passthoughKeys[i]]; | |
} | |
} | |
// collect this click | |
clicks.push([ target, clonedEvent ]); | |
return false; | |
}, | |
// dispatch events | |
triggerEvent = document.createEvent ? function(el, evt) { | |
// dispatch for non IE browsers | |
var e = document.createEvent("HTMLEvents"), k; | |
// event type, bubbling, cancelable | |
e.initEvent("click", true, true); | |
for (k in evt) { | |
if (evt.hasOwnProperty(k)) { | |
e[k] = evt[k]; | |
} | |
} | |
return !el.dispatchEvent(e); | |
} : function(el, evt) { | |
// dispatch for IE | |
var e = document.createEventObject(), k; | |
for (k in evt) { | |
if (evt.hasOwnProperty(k)) { | |
e[k] = evt[k]; | |
} | |
} | |
return el.fireEvent("onclick", e); | |
}; | |
// bind event listener to the document level object | |
if (document.addEventListener) { | |
document.addEventListener("click", capture, false); | |
} else { | |
document.attachEvent("onclick", capture); | |
} | |
return function() { | |
var i = 0, l = clicks.length; | |
// release the document level click listener | |
if (document.removeEventListener) { | |
document.removeEventListener("click", capture, false); | |
} else { | |
document.detachEvent("onclick", capture); | |
} | |
for (; i < l; i++) { | |
triggerEvent(clicks[i][0], clicks[i][1]); | |
} | |
}; | |
})(document); | |
// usage | |
// window.onload = releaseClicks; | |
// or just invoke releaseClicks(); | |
// or $(document).ready(function(){ | |
// // bind all events first | |
// releaseClicks(); | |
// }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment