Skip to content
This repository

JavaScript Polyfills, Shims and More

branch: master

polyfill - JavaScript Polyfills, Shims and More

  • A shim lets you write the same code across all browsers by implementing a new API in downlevel browsers.
  • A polyfill is a shim or collection of shims (and a catchy name).
  • A prollyfill is a shim for a proposed API
  • A helper helps write cross-browser code where a true API shim/polyfill is not possible.

Note that my general approach to polyfills is not to produce 100% compliant behavior, but to provide a broad subset of functionality so that, where possible, cooperative code can be written to take advantage of new APIs. No assumptions should be made about security or edge cases. It is preferrable to use a shim where it is possible to create one on supported browsers. If not possible, a helper should be used that lets the same code be used in all browsers.

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

ECMAScript / JavaScript Polyfills

ECMAScript 5 - Most recent standard, supported by all modern browsers. Frozen.

ECMAScript 6 - Based on nearly complete draft standard. Should be stable apart from bug fixes.

ECMAScript 7 - At the initial proposal/strawman stage. Here there be dragons.

JavaScript 1.X String Extras - ref

  • String prototype: trimLeft, trimRight, quote

Web Standards / Browser Compat

script - unit tests

Bundled together; nearly every page I create needs at least some of these. These will change over time, and going forward I will only target IE8 and later.

  • XMLHttpRequest (for IE6-)
  • FormData (for IE9-)
  • Selector API (for IE7-) - adapted from Paul Young
    • element = document.querySelector(selector)
    • elementArray = document.querySelectorAll(selector)
  • DOM Events
    • Where EventTarget is window, document, or any element:
    • EventTarget.addEventListener(event, handler) - for IE8+
    • EventTarget.removeEventListener(event, handler) - for IE8+
    • window.addEvent(EventTarget, event, handler) - helper for IE7- support - adapted from QuirksMode
    • window.removeEvent(EventTarget, event, handler) - helper for IE7- support - adapted from QuirksMode
    • Event.currentTarget
    • Event.eventPhase
    • Event.bubbles
    • Event.cancelable
    • Event.timeStamp
    • Event.defaultPrevented
    • Event.stopPropagation()
    • Event.cancelBubble()
  • DOM Miscellany
    • document.head
    • elementArray = document.getElementsByClassName(classNames)
  • HTML Web Application APIs (for IE9-)
    • encodedString = window.btoa(binaryString) - Base64 Encode
    • binaryString = window.atob(encodedString) - Base64 Decode
  • HTML5 Infrastructure - classListspec, relListspec
    • tokenList = elem.classList - for IE8+
    • tokenList = elem.relList - for IE8+
    • tokenList = window.getClassList(element) - helper for IE7- support
    • tokenList = window.getRelList(element) - helper for IE7- support
    • tokenList.length
    • tokenList.item(index)
    • tokenList.contains(token)
    • tokenList.add(token)
    • tokenList.remove(token)
    • tokenList.toggle(token)
  • W3C Timing control for script-based animations - demo page
    • id = window.requestAnimationFrame()
    • window.cancelAnimationFrame(id)
  • Efficient Script Yielding
    • id = setImmediate(callback, args...)
    • clearImmediate(id)
  • dataset and data-* attributes spec (for IE8+, not available in IE7-)
    • str = element.dataset[key] - yields undefined if data-key attribute not present
    • element.dataset[key] = str - fails unless data-key attribute already present


script - unit tests - draft spec - See script for cross-browser quirks

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

URL objects have properties:

  • href
  • origin
  • protocol
  • username
  • password
  • host
  • hostname
  • port
  • pathname
  • search
  • searchParams
    • append(name, value)
    • delete(name)
    • get(name)
    • getAll(name)
    • has(name)
    • set(name, value)
  • hash

W3C Keyboard Events (polyfill)

script - demo page - draft spec

// Adds the following properties to each KeyboardEvent:

// You can get a label for the key using:

// IE7- only: In your keydown/keyup handler, call this in your handler
// before accessing the `code` or `location` properties:

W3C Geolocation API

script - demo page - spec - uses

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


Obsolete and Unmaintained Polyfills

Something went wrong with that request. Please try again.