Skip to content

Instantly share code, notes, and snippets.

@tomblanchard
Created June 9, 2014 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomblanchard/1b1a094d9cb04e35e87e to your computer and use it in GitHub Desktop.
Save tomblanchard/1b1a094d9cb04e35e87e to your computer and use it in GitHub Desktop.
Native JS API pollyfills
/**
`classList` pollyfill:
https://github.com/remy/polyfills/blob/master/classList.js
*/
!function(){function e(a){this.el=a;for(var c=a.className.replace(/^\s+|\s+$/g,"").split(/\s+/),d=0;d<c.length;d++)b.call(this,c[d])}function f(a,b,c){Object.defineProperty?Object.defineProperty(a,b,{get:c}):a.__defineGetter__(b,c)}if(!("undefined"==typeof window.Element||"classList"in document.documentElement)){var a=Array.prototype,b=a.push,c=a.splice,d=a.join;e.prototype={add:function(a){this.contains(a)||(b.call(this,a),this.el.className=this.toString())},contains:function(a){return-1!=this.el.className.indexOf(a)},item:function(a){return this[a]||null},remove:function(a){if(this.contains(a)){for(var b=0;b<this.length&&this[b]!=a;b++);c.call(this,b,1),this.el.className=this.toString()}},toString:function(){return d.call(this," ")},toggle:function(a){return this.contains(a)?this.remove(a):this.add(a),this.contains(a)}},window.DOMTokenList=e,f(Element.prototype,"classList",function(){return new e(this)})}}();
/**
`addEventListener` pollyfill:
https://gist.github.com/eirikbacker/2864711
*/
!function(a,b){function c(a){var c=b[a];b[a]=function(a){return e(c(a))}}function d(b,c,d){return(d=this).attachEvent("on"+b,function(b){var b=b||a.event;b.preventDefault=b.preventDefault||function(){b.returnValue=!1},b.stopPropagation=b.stopPropagation||function(){b.cancelBubble=!0},c.call(d,b)})}function e(a,b){if(b=a.length)for(;b--;)a[b].addEventListener=d;else a.addEventListener=d;return a}a.addEventListener||(e([b,a]),"Element"in a?a.Element.prototype.addEventListener=d:(b.attachEvent("onreadystatechange",function(){e(b.all)}),c("getElementsByTagName"),c("getElementById"),c("createElement"),e(b.all)))}(window,document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment