SlideShare a Scribd company logo
Doris Chen Ph.D.
Developer Evangelist
Microsoft
doris.chen@microsoft.com
@doristchen
Building Web Sites that
Work Everywhere
Who am I?
 Developer Evangelist at Microsoft based in Silicon
Valley, CA
 Blog: http://blogs.msdn.com/b/dorischen/
 Twitter @doristchen
 Email: doris.chen@microsoft.com
 Has over 18 years of experience in the software industry
focusing on web technologies
 Spoke and published widely at OSCON, Fluent, HTML5
DevConf, JavaOne, O'Reilly, Silicon Valley Code Camp,
and worldwide User Groups meetings
 Doris received her Ph.D. from the University of California
at Los Angeles (UCLA)
Agenda
PAGE 3
Cross Browser Testing
Browser Detection and User Agent
Polyfills and Fallbacks
Summary
Feature Detection
CSS3 and Vendor Prefixes
Cross Browser Testing
4
Building Web Sites that Work Everywhere
MICROSOFT CONFIDENTIAL
Interoperable intersection
TheMobileWeb
Testing Tools
 Site Scan (Free)
 Reports back on common coding problems
 https://dev.windows.com/en-us/microsoft-edge/tools/staticscan/
 Browser screenshots (Free)
 Take screenshots of your site in a selection of common browsers and devices
 https://dev.windows.com/en-us/microsoft-edge/tools/screenshots/
 Windows virtual machines (Free)
 free downloads of Windows virtual machines used to test IE6 - IE11
 Need to have a platform for hosting virtual machines: VirtualBox, VMware Fusion
and Parallels
 https://dev.windows.com/en-us/microsoft-edge/tools/vms/windows/
 BrowserStack
 A paid online service that gives you access to hundreds of virtual machines
 Quickly test sites from your browser without installing any actual virtual
machines
 Supports local tunneling, so your site doesn't have to be live
 http://www.browserstack.com/
Testing Tools
Demo
CSS3 and Vendor Prefixes
12
Vendor Prefixes
 Browser vendors oftentimes use vendor-specific prefixes to
implement new features before they are W3C
recommendations
 Once a feature or property is a W3C recommendation,
browsers usually will support the non-prefixed versions
 Examples of vendor prefixes:
 Vendor prefixes are still needed to support older browser
versions, even after the browser vendors have adopted the
non-prefixed versions in newer versions
13
-ms-
-moz-
-o-
-webkit-
Microsoft
Mozilla
Opera
Safari and other WebKit-based browsers
When to Use CSS Prefixes
Common properties & values that need prefixed:
animation
box-sizing
Flexbox layout module properties
CSS regions
transition
transform
CSS grid layout
filter
calc() unit value
16
When to NOT Use Prefixes
Common properties that don't need prefixed:
border-radius
box-shadow
font-face
opacity
text-shadow
pointer-events
max-height
min-height
17
Prefix Tools
 Autoprefixer
 Parses CSS files and adds appropriate vendor prefixes
 Allows you to write CSS without worrying about what prefixes
you should use
 Vendor prefixes are pulled from caniuse.com
 Autoprefixer is a "post-processor", meaning it runs after your
Sass, Stylus or Less has been compiled
 Use Autoprefixer with Grunt
 https://autoprefixer.github.io/
 -prefix-free
 Use only unprefixed CSS properties
 Adding the current browser’s prefix to any CSS
 http://leaverou.github.io/prefixfree/#test-drive
18
Prefix tools
Demo
Browser Detection and User Agent
22
Browser Fragmentation
Fragmentation
 Varying Levels of Support
 Across browsers
 Across browser versions
 New versions released
constantly
 Browser detection doesn’t
work
 Fixes based on assumptions
 Full-time job tracking
changes
Browser Detection: User Agent
 Each browser User Agent through the years tried to
mimic others
 Lead to very complicated logic when UA sniffing
 Many browsers try to act like other browsers to claim
they support each other
 In the past, the user agent string was used to change
code per browser (sometimes via a JavaScript plugin)
 No longer a recommended technique
 Much better technique is to use feature detection
26
User-Agent Strings
Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/39.0.2171.71 Safari/537.36 Edge/12.0
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/600.1.25 (KHTML, like Gecko)
Version/8.0 Safari/600.1.25
Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/40.0.2214.93 Safari/537.36
Mozilla/5.0 (Windows NT 10.0; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0
User Agent Sniffing
 Very inexact and can easily be spoofed
 You can end up with very messy browser testing logic:
if (ie) {
if (ie.version === 7) { ... }
} else if (firefox) {
...
} else { ... } // and on and on
 Much less maintainable as browsers evolve
31
Feature Detection
Feature Detection
 Based on what browsers support, not their versions
 Check for the feature you want to use
 Object, Method, Property, Behavior
 Detect for standards-based features
first
 Browsers often support both standards and
legacy
 Standards are your most stable ground to build
upon
 Dynamically load custom code to mimic missing
features
Why not Check for a
Browser?
// If not IE then use addEventListener…
if (navigator.userAgent.indexOf("MSIE") == -1) {
window.addEventListener( “load”, popMessage, false );
} else if (window.attachEvent) {
window.attachEvent( “onload”, popMessage);
}
Bad
Why not Check for a
Browser?
if (window.addEventListener) {
window.addEventListener( “load”, popMessage,
false );
} else if (window.attachEvent) {
window.attachEvent( “onload”, popMessage);
}
Good
Feature Detection
 Best option in my opinion for this…
 http://www.modernizr.com/
 Best feature detection Support
 Detects:
 All major HTML5 features
 All major CSS3 features
 Constantly updated
 Can create a bundle of the tests needed
 Utilize one of the many Modernizr test scripts
 Create a custom script
 Also has polyfills to keep older browsers
supported
Get Custom Build
function(){
var
sheet, bool,
head = docHead || docElement,
style = document.createElement("style"),
impl = document.implementation || { hasFeature: function() { return false; } };
style.type = 'text/css';
head.insertBefore(style, head.firstChild);
sheet = style.sheet || style.styleSheet;
var supportAtRule = impl.hasFeature('CSS2', '') ?
function(rule) {
if (!(sheet && rule)) return false;
var result = false;
try {
sheet.insertRule(rule, 0);
result = (/src/i).test(sheet.cssRules[0].cssText);
sheet.deleteRule(sheet.cssRules.length - 1);
} catch(e) { }
return result;
} :
function(rule) {
if (!(sheet && rule)) return false;
sheet.cssText = rule;
return sheet.cssText.length !== 0 && (/src/i).test(sheet.cssText) &&
sheet.cssText
.replace(/r+|n+/g, '')
.indexOf(rule.split(' ')[0]) === 0;
};
bool = supportAtRule('@font-face { font-family: "font"; src: url(data:,); }');
head.removeChild(style);
return bool;
};
Test for @font-face
You can do this
Test for @font-face
Or ….
if (Modernizr.fontface){
…
}
Demo
Polyfills and Fallbacks
Polyfills
 What are they?
 Typically JavaScript, HTML & CSS code
 What do they do?
 Provides support for missing features
 When do you use them?
 Use them to fallback gracefully or mimic
functionality
 Modernizr will polyfill HTML5 elements automatically
 Help load polyfills for nearly every feature it detects
 Create your own polyfill, or use existing ones
Example Manual Polyfill
if (!window.localStorage) {
window.localStorage = {
getItem: function (key) { /* ... */ },
setItem: function (key, val) { /* ... */ },
clear: function () { /* ... */ }
/* ... */
};
}
53
Example Polyfill with Modernizr
Modernizr.load({
test: Modernizr.geolocation,
yep: 'geo.js',
nope: 'geo-polyfill.js'
});
54
Polyfills: Examples
Video Tags
HTML5 Video & Audio
<audio <video
src= src= The url to the audio or video
width= The width of the video element
height= The height of the video element
poster= The url to the thumbnail of the video
preload= preload= (none, metadata, auto) Start downloading
autoplay autoplay Audio or video should play immediately
loop loop Audio or video should return to start and play
controls controls Will show controls (play, pause, scrub bar)
> >
… …
</audio> </video>
Compatibility Table
HTML5 Audio
MP3
audio
support
Yes
Yes Yes Yes Yes Yes
WAV PCM
audio
support
Yes
No Yes Yes Yes Yes
AAC audio
format
Yes
Yes Yes(*) Yes Yes Yes
Compatibility Table
HTML5 <video>
VP8
(WebM)
video
support
Yes (*)
Yes
Yes No (*) Yes Yes
H.264
video
format
(MPEG-4)
Yes
Yes Yes Yes Yes
Ogg/
Theora
Consider No Yes No Yes Yes
Elements With Fall Backs
PAGE 60
Browsers won’t render elements
they don’t understand...
But, they will render what’s
between those elements
For example:
<video src=“video.mp4”>
What will it render?
</video>
HTML5 <video> : Degrading Gracefully
<video src="video.mp4" poster="movie.jpg"
height="480" width="640"
autoplay autobuffer controls loop>
<!-- Flash/Silverlight video here -->
<script type="text/javascript">
var so = new SWFObject(“video.swf”);
so.addParam("allowFullScreen","true");
so.addVariable("autostart","true");
…
</script>
</video>
 Multiple video sources to support multiple browsers
Plugin Free Experience:
http://www.worldstarhiphop.com/vi
deos/video.php?v=wshhNPy952Q9
wUk3n353
Demo
Take Away
 Avoid browser detection, User Agent Sniffing
 Browsers change
 Varying levels of feature support across all major
browsers
 Use feature detection
 Check for the feature you want to use
 Detect for standards first
 Use Modernizr – http://modernizr.com
 Cleaner code & they’ve done the work for you
 Polyfills and Fallbacks
 mimics a standard API to avoid a rewrite

More Related Content

PDF
Angular mobile angular_u
PDF
Angular or Backbone: Go Mobile!
PDF
Practical tipsmakemobilefaster oscon2016
PDF
Lastest Trends in Web Development
PDF
Learn about Eclipse e4 from Lars Vogel at SF-JUG
PDF
Great Responsive-ability Web Design
PDF
Web Components v1
KEY
Mobile HTML, CSS, and JavaScript
Angular mobile angular_u
Angular or Backbone: Go Mobile!
Practical tipsmakemobilefaster oscon2016
Lastest Trends in Web Development
Learn about Eclipse e4 from Lars Vogel at SF-JUG
Great Responsive-ability Web Design
Web Components v1
Mobile HTML, CSS, and JavaScript

What's hot (20)

PPTX
Cartegraph Live HTML, CSS, JavaScript and jQuery Training
PDF
jQuery Proven Performance Tips & Tricks
PPTX
Introduction to jQuery
PDF
C# Advanced L09-HTML5+ASP
PPT
High Performance Ajax Applications
PPTX
Responsive Layout Frameworks for XPages Application UI
PDF
[2015/2016] Backbone JS
PDF
[2015/2016] JavaScript
PDF
Js Saturday 2013 your jQuery could perform better
PDF
HTML5 and the dawn of rich mobile web applications
PDF
[2015/2016] Require JS and Handlebars JS
PDF
Introduction to JavaScript
PDF
Apache Cordova 4.x
PDF
Modern development paradigms
PDF
Keypoints html5
PDF
HTML5 & CSS3 refresher for mobile apps
PDF
Backbone JS for mobile apps
PPTX
JsViews - Next Generation jQuery Templates
PDF
HTML5 and the dawn of rich mobile web applications pt 1
PDF
Building an HTML5 Video Player
Cartegraph Live HTML, CSS, JavaScript and jQuery Training
jQuery Proven Performance Tips & Tricks
Introduction to jQuery
C# Advanced L09-HTML5+ASP
High Performance Ajax Applications
Responsive Layout Frameworks for XPages Application UI
[2015/2016] Backbone JS
[2015/2016] JavaScript
Js Saturday 2013 your jQuery could perform better
HTML5 and the dawn of rich mobile web applications
[2015/2016] Require JS and Handlebars JS
Introduction to JavaScript
Apache Cordova 4.x
Modern development paradigms
Keypoints html5
HTML5 & CSS3 refresher for mobile apps
Backbone JS for mobile apps
JsViews - Next Generation jQuery Templates
HTML5 and the dawn of rich mobile web applications pt 1
Building an HTML5 Video Player
Ad

Similar to Building Web Sites that Work Everywhere (20)

PDF
Echo HTML5
PPTX
Session on Selenium 4 : What’s coming our way? by Hitesh Prajapati
PPTX
Selenium 4 - What's coming our way - v1.0.pptx
KEY
HTML5 vs Silverlight
PPT
Vs2005p
PDF
Quest to the best test automation for low code development platform kherrazi ...
PPTX
HTML5 and CSS3 Techniques You Can Use Today
PDF
orcreatehappyusers
PDF
orcreatehappyusers
PPTX
Html5 & less css
PPT
Joomla Day Austin Part 4
PDF
Session on Selenium Powertools by Unmesh Gundecha
PDF
Get Ahead with HTML5 on Moible
PDF
Intro to mobile web application development
PPTX
Ethical hacking Chapter 10 - Exploiting Web Servers - Eric Vanderburg
PDF
Robot Framework Introduction & Sauce Labs Integration
PPTX
Selenium Tutorial for Beginners - TIB Academy
PDF
HTML5 Intoduction for Web Developers
PPTX
HTML5 Bootcamp: Essential HTML, CSS, & JavaScript
PDF
Familiar HTML5 - 事例とサンプルコードから学ぶ 身近で普通に使わているHTML5
Echo HTML5
Session on Selenium 4 : What’s coming our way? by Hitesh Prajapati
Selenium 4 - What's coming our way - v1.0.pptx
HTML5 vs Silverlight
Vs2005p
Quest to the best test automation for low code development platform kherrazi ...
HTML5 and CSS3 Techniques You Can Use Today
orcreatehappyusers
orcreatehappyusers
Html5 & less css
Joomla Day Austin Part 4
Session on Selenium Powertools by Unmesh Gundecha
Get Ahead with HTML5 on Moible
Intro to mobile web application development
Ethical hacking Chapter 10 - Exploiting Web Servers - Eric Vanderburg
Robot Framework Introduction & Sauce Labs Integration
Selenium Tutorial for Beginners - TIB Academy
HTML5 Intoduction for Web Developers
HTML5 Bootcamp: Essential HTML, CSS, & JavaScript
Familiar HTML5 - 事例とサンプルコードから学ぶ 身近で普通に使わているHTML5
Ad

More from Doris Chen (20)

PDF
OSCON Presentation: Developing High Performance Websites and Modern Apps with...
PDF
Practical Performance Tips and Tricks to Make Your HTML/JavaScript Apps Faster
PDF
Developing High Performance Websites and Modern Apps with JavaScript and HTML5
PDF
What Web Developers Need to Know to Develop Native HTML5/JS Apps
PDF
Windows 8 Opportunity
PDF
Wins8 appfoforweb fluent
PDF
Develop High Performance Windows 8 Application with HTML5 and JavaScriptHigh ...
PDF
What Web Developers Need to Know to Develop Windows 8 Apps
PDF
Building Beautiful and Interactive Metro apps with JavaScript, HTML5 & CSS3
PDF
Building Beautiful and Interactive Windows 8 apps with JavaScript, HTML5 & CSS3
PDF
Introduction to CSS3
PDF
Building Beautiful and Interactive Metro apps with JavaScript, HTML5 & CSS3
PDF
Practical HTML5: Using It Today
PDF
Dive Into HTML5
PDF
Practical HTML5: Using It Today
PDF
HTML 5 Development for Windows Phone and Desktop
PDF
Dive into HTML5: SVG and Canvas
PDF
Performance Optimization and JavaScript Best Practices
PDF
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
PDF
Develop Netflix Movie Search App using jQuery, OData, JSONP and Netflix Techn...
OSCON Presentation: Developing High Performance Websites and Modern Apps with...
Practical Performance Tips and Tricks to Make Your HTML/JavaScript Apps Faster
Developing High Performance Websites and Modern Apps with JavaScript and HTML5
What Web Developers Need to Know to Develop Native HTML5/JS Apps
Windows 8 Opportunity
Wins8 appfoforweb fluent
Develop High Performance Windows 8 Application with HTML5 and JavaScriptHigh ...
What Web Developers Need to Know to Develop Windows 8 Apps
Building Beautiful and Interactive Metro apps with JavaScript, HTML5 & CSS3
Building Beautiful and Interactive Windows 8 apps with JavaScript, HTML5 & CSS3
Introduction to CSS3
Building Beautiful and Interactive Metro apps with JavaScript, HTML5 & CSS3
Practical HTML5: Using It Today
Dive Into HTML5
Practical HTML5: Using It Today
HTML 5 Development for Windows Phone and Desktop
Dive into HTML5: SVG and Canvas
Performance Optimization and JavaScript Best Practices
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
Develop Netflix Movie Search App using jQuery, OData, JSONP and Netflix Techn...

Recently uploaded (20)

PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Advanced IT Governance
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
KodekX | Application Modernization Development
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Cloud computing and distributed systems.
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPT
Teaching material agriculture food technology
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
NewMind AI Monthly Chronicles - July 2025
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Spectral efficient network and resource selection model in 5G networks
NewMind AI Weekly Chronicles - August'25 Week I
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Advanced IT Governance
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
KodekX | Application Modernization Development
MYSQL Presentation for SQL database connectivity
Cloud computing and distributed systems.
Review of recent advances in non-invasive hemoglobin estimation
Teaching material agriculture food technology
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
20250228 LYD VKU AI Blended-Learning.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Understanding_Digital_Forensics_Presentation.pptx

Building Web Sites that Work Everywhere

  • 1. Doris Chen Ph.D. Developer Evangelist Microsoft doris.chen@microsoft.com @doristchen Building Web Sites that Work Everywhere
  • 2. Who am I?  Developer Evangelist at Microsoft based in Silicon Valley, CA  Blog: http://blogs.msdn.com/b/dorischen/  Twitter @doristchen  Email: doris.chen@microsoft.com  Has over 18 years of experience in the software industry focusing on web technologies  Spoke and published widely at OSCON, Fluent, HTML5 DevConf, JavaOne, O'Reilly, Silicon Valley Code Camp, and worldwide User Groups meetings  Doris received her Ph.D. from the University of California at Los Angeles (UCLA)
  • 3. Agenda PAGE 3 Cross Browser Testing Browser Detection and User Agent Polyfills and Fallbacks Summary Feature Detection CSS3 and Vendor Prefixes
  • 7. Testing Tools  Site Scan (Free)  Reports back on common coding problems  https://dev.windows.com/en-us/microsoft-edge/tools/staticscan/  Browser screenshots (Free)  Take screenshots of your site in a selection of common browsers and devices  https://dev.windows.com/en-us/microsoft-edge/tools/screenshots/  Windows virtual machines (Free)  free downloads of Windows virtual machines used to test IE6 - IE11  Need to have a platform for hosting virtual machines: VirtualBox, VMware Fusion and Parallels  https://dev.windows.com/en-us/microsoft-edge/tools/vms/windows/  BrowserStack  A paid online service that gives you access to hundreds of virtual machines  Quickly test sites from your browser without installing any actual virtual machines  Supports local tunneling, so your site doesn't have to be live  http://www.browserstack.com/
  • 9. CSS3 and Vendor Prefixes 12
  • 10. Vendor Prefixes  Browser vendors oftentimes use vendor-specific prefixes to implement new features before they are W3C recommendations  Once a feature or property is a W3C recommendation, browsers usually will support the non-prefixed versions  Examples of vendor prefixes:  Vendor prefixes are still needed to support older browser versions, even after the browser vendors have adopted the non-prefixed versions in newer versions 13 -ms- -moz- -o- -webkit- Microsoft Mozilla Opera Safari and other WebKit-based browsers
  • 11. When to Use CSS Prefixes Common properties & values that need prefixed: animation box-sizing Flexbox layout module properties CSS regions transition transform CSS grid layout filter calc() unit value 16
  • 12. When to NOT Use Prefixes Common properties that don't need prefixed: border-radius box-shadow font-face opacity text-shadow pointer-events max-height min-height 17
  • 13. Prefix Tools  Autoprefixer  Parses CSS files and adds appropriate vendor prefixes  Allows you to write CSS without worrying about what prefixes you should use  Vendor prefixes are pulled from caniuse.com  Autoprefixer is a "post-processor", meaning it runs after your Sass, Stylus or Less has been compiled  Use Autoprefixer with Grunt  https://autoprefixer.github.io/  -prefix-free  Use only unprefixed CSS properties  Adding the current browser’s prefix to any CSS  http://leaverou.github.io/prefixfree/#test-drive 18
  • 15. Browser Detection and User Agent 22
  • 17. Fragmentation  Varying Levels of Support  Across browsers  Across browser versions  New versions released constantly  Browser detection doesn’t work  Fixes based on assumptions  Full-time job tracking changes
  • 18. Browser Detection: User Agent  Each browser User Agent through the years tried to mimic others  Lead to very complicated logic when UA sniffing  Many browsers try to act like other browsers to claim they support each other  In the past, the user agent string was used to change code per browser (sometimes via a JavaScript plugin)  No longer a recommended technique  Much better technique is to use feature detection 26
  • 19. User-Agent Strings Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36 Edge/12.0 Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/600.1.25 (KHTML, like Gecko) Version/8.0 Safari/600.1.25 Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.93 Safari/537.36 Mozilla/5.0 (Windows NT 10.0; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0
  • 20. User Agent Sniffing  Very inexact and can easily be spoofed  You can end up with very messy browser testing logic: if (ie) { if (ie.version === 7) { ... } } else if (firefox) { ... } else { ... } // and on and on  Much less maintainable as browsers evolve 31
  • 22. Feature Detection  Based on what browsers support, not their versions  Check for the feature you want to use  Object, Method, Property, Behavior  Detect for standards-based features first  Browsers often support both standards and legacy  Standards are your most stable ground to build upon  Dynamically load custom code to mimic missing features
  • 23. Why not Check for a Browser? // If not IE then use addEventListener… if (navigator.userAgent.indexOf("MSIE") == -1) { window.addEventListener( “load”, popMessage, false ); } else if (window.attachEvent) { window.attachEvent( “onload”, popMessage); } Bad
  • 24. Why not Check for a Browser? if (window.addEventListener) { window.addEventListener( “load”, popMessage, false ); } else if (window.attachEvent) { window.attachEvent( “onload”, popMessage); } Good
  • 25. Feature Detection  Best option in my opinion for this…  http://www.modernizr.com/
  • 26.  Best feature detection Support  Detects:  All major HTML5 features  All major CSS3 features  Constantly updated  Can create a bundle of the tests needed  Utilize one of the many Modernizr test scripts  Create a custom script  Also has polyfills to keep older browsers supported
  • 28. function(){ var sheet, bool, head = docHead || docElement, style = document.createElement("style"), impl = document.implementation || { hasFeature: function() { return false; } }; style.type = 'text/css'; head.insertBefore(style, head.firstChild); sheet = style.sheet || style.styleSheet; var supportAtRule = impl.hasFeature('CSS2', '') ? function(rule) { if (!(sheet && rule)) return false; var result = false; try { sheet.insertRule(rule, 0); result = (/src/i).test(sheet.cssRules[0].cssText); sheet.deleteRule(sheet.cssRules.length - 1); } catch(e) { } return result; } : function(rule) { if (!(sheet && rule)) return false; sheet.cssText = rule; return sheet.cssText.length !== 0 && (/src/i).test(sheet.cssText) && sheet.cssText .replace(/r+|n+/g, '') .indexOf(rule.split(' ')[0]) === 0; }; bool = supportAtRule('@font-face { font-family: "font"; src: url(data:,); }'); head.removeChild(style); return bool; }; Test for @font-face You can do this
  • 29. Test for @font-face Or …. if (Modernizr.fontface){ … }
  • 30. Demo
  • 32. Polyfills  What are they?  Typically JavaScript, HTML & CSS code  What do they do?  Provides support for missing features  When do you use them?  Use them to fallback gracefully or mimic functionality  Modernizr will polyfill HTML5 elements automatically  Help load polyfills for nearly every feature it detects  Create your own polyfill, or use existing ones
  • 33. Example Manual Polyfill if (!window.localStorage) { window.localStorage = { getItem: function (key) { /* ... */ }, setItem: function (key, val) { /* ... */ }, clear: function () { /* ... */ } /* ... */ }; } 53
  • 34. Example Polyfill with Modernizr Modernizr.load({ test: Modernizr.geolocation, yep: 'geo.js', nope: 'geo-polyfill.js' }); 54
  • 36. HTML5 Video & Audio <audio <video src= src= The url to the audio or video width= The width of the video element height= The height of the video element poster= The url to the thumbnail of the video preload= preload= (none, metadata, auto) Start downloading autoplay autoplay Audio or video should play immediately loop loop Audio or video should return to start and play controls controls Will show controls (play, pause, scrub bar) > > … … </audio> </video>
  • 37. Compatibility Table HTML5 Audio MP3 audio support Yes Yes Yes Yes Yes Yes WAV PCM audio support Yes No Yes Yes Yes Yes AAC audio format Yes Yes Yes(*) Yes Yes Yes
  • 38. Compatibility Table HTML5 <video> VP8 (WebM) video support Yes (*) Yes Yes No (*) Yes Yes H.264 video format (MPEG-4) Yes Yes Yes Yes Yes Ogg/ Theora Consider No Yes No Yes Yes
  • 39. Elements With Fall Backs PAGE 60 Browsers won’t render elements they don’t understand... But, they will render what’s between those elements For example: <video src=“video.mp4”> What will it render? </video>
  • 40. HTML5 <video> : Degrading Gracefully <video src="video.mp4" poster="movie.jpg" height="480" width="640" autoplay autobuffer controls loop> <!-- Flash/Silverlight video here --> <script type="text/javascript"> var so = new SWFObject(“video.swf”); so.addParam("allowFullScreen","true"); so.addVariable("autostart","true"); … </script> </video>  Multiple video sources to support multiple browsers
  • 42. Take Away  Avoid browser detection, User Agent Sniffing  Browsers change  Varying levels of feature support across all major browsers  Use feature detection  Check for the feature you want to use  Detect for standards first  Use Modernizr – http://modernizr.com  Cleaner code & they’ve done the work for you  Polyfills and Fallbacks  mimics a standard API to avoid a rewrite