mirror of
https://github.com/jsbin/jsbin.git
synced 2026-01-25 15:38:56 +00:00
43 lines
1.1 KiB
JavaScript
43 lines
1.1 KiB
JavaScript
/**
|
|
* Utilities & polyfills
|
|
*/
|
|
|
|
var prependChild = function(elem, child) { elem.insertBefore(child, elem.firstChild); };
|
|
|
|
var addEvent = function(elem, event, fn) {
|
|
if (elem.addEventListener) {
|
|
elem.addEventListener(event, fn, false);
|
|
} else {
|
|
elem.attachEvent("on" + event, function() {
|
|
// set the this pointer same as addEventListener when fn is called
|
|
return(fn.call(elem, window.event));
|
|
});
|
|
}
|
|
};
|
|
|
|
if (!window.location.origin) window.location.origin = window.location.protocol+"//"+window.location.host;
|
|
|
|
var throttle = function (fn, delay) {
|
|
var timer = null;
|
|
var throttled = function () {
|
|
var context = this, args = arguments;
|
|
throttled.cancel();
|
|
throttled.timer = setTimeout(function () {
|
|
fn.apply(context, args);
|
|
}, delay);
|
|
};
|
|
|
|
throttled.cancel = function () {
|
|
clearTimeout(throttled.timer);
|
|
};
|
|
|
|
return throttled;
|
|
};
|
|
|
|
var cleanse = function (s) {
|
|
return (s||'').replace(/[<&]/g, function (m) { return {'&':'&','<':'<'}[m];});
|
|
};
|
|
|
|
var getIframeWindow = function (iframeElement) {
|
|
return iframeElement.contentWindow || iframeElement.contentDocument.parentWindow;
|
|
}; |