inexorabletash / polyfill

JavaScript Polyfills, Shims and More
Other
1.36k stars 354 forks source link
javascript polyfills

polyfill - JavaScript and Web Polyfills

This is a collection of polyfills covering web platform features, from those defined as part of the ECMAScript standard to new web browser functionality. Most are for features shipping in major browsers. A few are experimental and called out as such, subject to change at any time.

My philosophy is that it's better to write future-looking code that takes advantage of new Web platform APIs where possible, and fill in the gaps with polyfills. There is no effort to produce 100% compliant behavior, or to completely hide differences in browser behavior.

I use these in various pages on my sites; most are by me, or I have at least tweaked them. A more comprehensive list of polyfills can be found at The All-In-One Entirely-Not-Alphabetical No-Bullshit Guide to HTML5 Fallbacks by Paul Irish.

Getting the Code

You're already here! Great, just download it, or use:

git: git clone https://github.com/inexorabletash/polyfill.git

bower: bower install js-polyfills

npm: npm install js-polyfills

It is not packaged as Node.js module(s); there's nothing to require(), this is just for distribution.

Or just include scripts directly in your page via CDN (c/o RawGit):

<script src="https://cdn.rawgit.com/inexorabletash/polyfill/$TAGNAME/polyfill.min.js"></script>

(look at Releases for the tag name, e.g. "v1.2.3")

Files

The polyfills are roughly split up into files matching 1:1 with Web standards (specifications, living standards documents, etc). So there is html.js for HTML, dom.js for DOM, etc.

Since I generally use several in my hobby projects, bundled/minified versions are available:

Minification is done via https://github.com/mishoo/UglifyJS2

Some of the files use console.assert() calls to catch bugs during development. These are automatically removed from the included minified versions. If you use your own minifying processor it may cause to assertions to appear when unnecessary function names are stripped. You can safely remove these lines as part of a build step (e.g. using grep -V), or use a minifier that does this automatically. For UglifyJS2 the option is: drop_console

ECMAScript / JavaScript Polyfills

ECMAScript 5 - Previous standard, supported by browsers circa 2012..

ECMAScript 2015 - Previous standard, supported by browsers circa 2016.

ECMAScript 2016 - Previous standard, supported by browsers circa 2017.

ECMAScript 2017 - Most recent standard. Implementation in progress or complete in latest browsers.

ECMAScript proposed - Proposals for future editions of the standard. Here there be dragons.

JavaScript 1.X String Extras - ref

HTML

script - tests - living standard

DOM

script - tests - living standard

Fetch

script - tests - living standard

Example:

fetch('http://example.com/foo.json')
  .then(function(response) { return response.json(); })
  .then(function(data) { console.log(data); });

Supported:

XMLHttpRequest

script - tests - living standard

CSS OM

script - spec

Polyfill for width and height in getBoundingClientRect() in IE8-

URL API

script - tests - living standard

var url = new URL(url, base);
var value = url.searchParams.get(name);
var valueArray = url.searchParams.getAll(name);
url.searchParams.append(name, valueOrValues);
url.searchParams.delete(name);

var p = new URLSearchParams('a=1&b=2');

Uncommon Polyfills

The following are of limited use and are not included in the web.js / polyfill.js bundled versions.

Timing

script

Keyboard Events

script - demo page - draft spec (also)

KeyboardEvent: code, key, location, KeyboardEvent.queryKeyCap(code)

IE7- only: Call window.identifyKey(keyboardEvent); in keydown/keyup handlers before accessing above properties.

more details

Geolocation API

script - demo page - spec - uses freegeoip.net

navigator.geolocation.getCurrentPosition(successCallback, errorCallback, options);
var watchId = navigator.geolocation.watchPosition(successCallback, errorCallback, options);
navigator.geolocation.clearWatch(watchId);

Obsolete

Obsolete and Unmaintained Polyfills