Polyfills For The Pragmatist

Jason Johnston

@lojjic, @css3pie

Sencha CSS3 PIE

Pauly who?

Also known as: shim, shiv, patch, hack

Term “polyfill” coined by Remy Sharp:

a piece of code (or plugin) that provides the technology that you, the developer, expect the browser to provide natively

Polyfill

FILL: fills in missing standard features or APIs when not natively available

POLY: can use multiple fallbacks depending on the browser and environment

Why are they needed?

“OldIE”

...but also newer browsers, to keep up with pace of HTML5

Wait a minute...

Don't most libraries smooth out browser incompatibilities?

Abstraction

You use standard code, not new APIs

Goal is to be invisible

Reasons To Use A Polyfill

Modernizr

http://modernizr.com

Modernizr

<html class="js no-flexbox no-flexbox-legacy no-canvas no-canvastext
    no-webgl no-touch no-geolocation postmessage no-websqldatabase
    no-indexeddb hashchange no-history draganddrop no-websockets
    no-rgba no-hsla no-multiplebgs no-backgroundsize no-borderimage
    no-borderradius no-boxshadow no-textshadow no-opacity
    no-cssanimations no-csscolumns no-cssgradients no-cssreflections
    no-csstransforms no-csstransforms3d no-csstransitions fontface
    generatedcontent no-video no-audio no-localstorage
    no-sessionstorage no-webworkers no-applicationcache no-svg
    no-inlinesvg no-smil no-svgclippaths">

Modernizr

YepNope.js

yepnope([{
    test : /*boolean(ish) - Something truthy that you want to test      */,
    yep  : /*array of strings | string - Things to load if test is true */,
    nope : /*array of strings | string - Things to load if test is false*/,
    both : /*array of strings | string - Load everytime (sugar)         */,
    load : /*array of strings | string - Load everytime (sugar)         */,
    callback : /*function ( testResult, key ) | object { key : fn }     */,
    complete : /*function                                               */
}, ... ]);

Modernizr

YepNope.js

yepnope({
    test : window.JSON,
    nope : 'json2.js'
});
yepnope({
    test: Modernizr.geolocation,
    yep: 'geo-styles.css',
    nope: ['geolocation-polyfill.js', 'no-geo-styles.css']
});

Modernizr

Also from the Modernizr community:

The All-In-One Entirely-Not-Alphabetical No-Bullshit Guide to HTML5 Fallbacks

https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-Browser-Polyfills

Things Polyfills Implement

Ways Polyfills Work

1. Pure Markup

Ways Polyfills Work

2. JavaScript Global Object Creation

Ways Polyfills Work

3. JavaScript Object/Prototype Modification

Ways Polyfills Work

4. Plugins

Ways Polyfills Work

5. CSS Parsing/Rewriting

Ways Polyfills Work

6. DOM Querying

Ways Polyfills Work

7. IE Behaviors (.htc)

Let's explore a few...

html5shim / html5shiv

https://github.com/aFarkas/html5shiv

HTML5 <video> / <audio>

JSON

https://github.com/douglascrockford/JSON-js

HTML5 <canvas>

Webshims Lib

https://github.com/aFarkas/webshim/

Webshims Lib

https://github.com/aFarkas/webshim/

<script src="js/jquery.js"></script>
<script src="js/Modernizr-yepnope.js"></script>
<script src="js-webshim/minified/polyfiller.js"></script>

<script>
    $.webshims.polyfill();

    // wait until load and then use the features
    $(function(){
        JSON.parse('...');
    });
</script>
Selectivizr

Selectivizr

http://selectivizr.com/

Selectivizr Support Chart
-prefix-free

-prefix-free

https://github.com/LeaVerou/prefixfree

IE7.js (and IE8.js, IE9.js)

http://code.google.com/p/ie7-js/

Respond.js

https://github.com/scottjehl/Respond

Flexie

http://flexiejs.com/

CSS Sandpaper

https://github.com/zoltan-dulac/cssSandpaper

CSS3 PIE

CSS3 PIE

http://css3pie.com/

...For The Pragmatist

Q: Should I use a polyfill?

A: Definitely maybe or maybe not.

Considerations

Sample Questions

“I'm deciding whether to use PIE instead of gracefully degrading. Should I use PIE?” NO.

“My project requires that IE users get the full design. Should I use PIE instead of cutting corner images?” MAYBE.

“My site needs to render as fast as possible, should I use PIE?” NO.

Assessing Performance

Polyfill creators don't typically publish performance stats; it's up to you to measure.

Common Factors

Assessing Performance

Polyfills that rewrite CSS

Assessing Performance

Polyfills that query the DOM

Compatibility Concerns

Don't Forget To Test!

In Conclusion,

Polyfills can greatly reduce development time and improve user experience.

But you need to weigh the pros and cons.

Polyfills will be with us long after OldIE is ancient history.