Slide 1

Slide 1 text

Choose Your Mobile Adventure Jason Grigsby • @grigs • cloudfour.com Slides: bit.ly/grigs-11-18-2011 http://www.flickr.com/photos/paperpariah/4245250519

Slide 2

Slide 2 text

Follow along @grigs_talks bit.ly/grigs-11-18-2011 After event: @grigs http://www.flickr.com/photos/stevegarfield/4247757731/

Slide 3

Slide 3 text

More devices every day

Slide 4

Slide 4 text

e Coming Zombie Apocalypse of Devices

Slide 5

Slide 5 text

Short Run Problem is Mobile Phones

Slide 6

Slide 6 text

Content & Services Car sketch: http://www.flickr.com/photos/cloppy/5081768461/ Long Run Problem

Slide 7

Slide 7 text

How do you deliver the right HTML, CSS and JavaScript to browsers?

Slide 8

Slide 8 text

Responsive Web Design Separate Mobile Site OR

Slide 9

Slide 9 text

Responsive Web Design http://www.flickr.com/photos/stuart-dootson/4024407198

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

@media screen and (min-width:480px) { /* CSS Rules */ } “screen” media type, we meet again! “min-” is a media query prefix. Rather intuitively, it means we want to query about a minimum width. “width” is a media feature we want to evaluate on the “screen” media type Unsurprisingly, there is also a “max-” prefix. These CSS rules will only get applied if the media query evaluates to TRUE Media Queries

Slide 14

Slide 14 text

Media Queries in Responsive Web Design

Slide 15

Slide 15 text

The site in a 960-pixel window e Problem with Fixed Layouts

Slide 16

Slide 16 text

e Problem with Fixed Layouts Left and right columns are 240 pixels wide...always The site viewed in a 700-pixel window The right column isn’t visible without scrolling

Slide 17

Slide 17 text

e Problem with Fixed Layouts The site viewed in a 1200-pixel window The entire width is constrained to 960 pixels

Slide 18

Slide 18 text

Target Context Result The size of the element in pixels. The size of the containing “context,” in pixels. Our new, proportional CSS rule, as a percentage.

Slide 19

Slide 19 text

960 pixels 240 pixels 240 pixels 460 pixels 960 pixels

Slide 20

Slide 20 text

960 pixels 240 pixels 240 pixels 460 pixels 960 pixels Target Context Result

Slide 21

Slide 21 text

960 pixels 240 pixels 240 pixels 460 pixels 960 pixels 240 pixels 960 pixels 25% The columns span a quarter of the page width. So this feels pretty intuitive, right?

Slide 22

Slide 22 text

960 pixels 240 pixels 240 pixels 460 pixels 960 pixels 240 pixels 960 pixels 25% The columns span a quarter of the page width. So this feels pretty intuitive, right? 260 pixels 960 pixels 27.0833333% #main { margin: 10px 260px 0 250px; } 250 pixels 960 pixels 26.0416667% Our current CSS rule

Slide 23

Slide 23 text

960 pixels 240 pixels 240 pixels 460 pixels 960 pixels 240 pixels 960 pixels 25% The columns span a quarter of the page width. So this feels pretty intuitive, right? 260 pixels 960 pixels 27.0833333% #main { margin: 10px 260px 0 250px; } 250 pixels 960 pixels 26.0416667% Our current CSS rule #main { margin: 10px 27.0833333% 0 26.0416667%; }

Slide 24

Slide 24 text

960 pixels 240 pixels 240 pixels 460 pixels 960 pixels 220 pixels 220 pixels

Slide 25

Slide 25 text

220 pixels 960 pixels 22.916667% 960 pixels 240 pixels 240 pixels 460 pixels 960 pixels 220 pixels 220 pixels

Slide 26

Slide 26 text

220 pixels 960 pixels 22.916667% 240 pixels 240 pixels 960 pixels fail! div.main

Slide 27

Slide 27 text

220 pixels 960 pixels 22.916667% 240 pixels 240 pixels 960 pixels fail! div.main

Slide 28

Slide 28 text

240 pixels 240 pixels 960 pixels fail! div.main

Slide 29

Slide 29 text

240 pixels 240 pixels 460 pixels 960 pixels div.main

Slide 30

Slide 30 text

240 pixels 240 pixels 460 pixels 960 pixels div.main 220 pixels 460 pixels 47.826087% new context: width of div#main, the containing element

Slide 31

Slide 31 text

img, object { max-width: 100%; } ka-pow! Flexible Images and Media

Slide 32

Slide 32 text

http://www.flickr.com/photos/adventuresof/111667571/ is sounds great. What’s the catch?

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

Looks can be deceiving Test mobile performance at http://blaze.io/mobile

Slide 36

Slide 36 text

Eight JavaScript files for this page. Total size of all JavaScript is 351K. Woah! There are 35 images. 2.4MB for images alone. See the pie charts by clicking on Show Statistics on the HAR Viewer page. What’s causing the page to be so slow?

Slide 37

Slide 37 text

Extremely long URL abbreviated This single iframe causes 47 files to be downloaded! Look inside ontap.html to find this code. Did you notice the map?

Slide 38

Slide 38 text

Extremely long URL abbreviated This single iframe causes 47 files to be downloaded! Look inside ontap.html to find this code. @media screen and (max-width:480px) { . . . #map {display:none;} } taps.css There are many more rules in the css file. The iframe has an id of map. This rule hides the Google Maps iframe by setting the display to none. Did you notice the map?

Slide 39

Slide 39 text

The taps.jpg file is 440.7K making it the largest file on the page. @media screen and (max-width:480px) { [Other CSS rules are here] .header {display:none;} } Same display:none issue with images

Slide 40

Slide 40 text

Original Resized K Saved % Saved Holmes 34.7K 8.1K 26.6K 76.6% Watson 39.0K 8.4K 30.6K 78.4% Mycroft 30.5K 6.7K 23.8K 78.0% Moriarty 43.4K 8.2K 35.2K 81.1% Adler 26.0K 6.6K 19.4K 74.6% Winter 34.7K 7.8K 26.9K 77.5% Total 208.3K 45.8K 162.5K 78.0% Flexible images are unnecessarily large images

Slide 41

Slide 41 text

Well, that’s disappointing. http://www.flickr.com/photos/kalexanderson/5421517469

Slide 42

Slide 42 text

Mobile First Responsive Web Design Separate Mobile Site OR

Slide 43

Slide 43 text

Mobile First Responsive Web Design http://www.flickr.com/photos/auyongcheemeng/95769332/

Slide 44

Slide 44 text

Mobile First Flips RWD Around http://bradfrostweb.com/blog/web/mobile-first-responsive-web-design/

Slide 45

Slide 45 text

/* Wider viewports/higher resolutions (e.g. desktop) */ @media screen and (min-width:481px) { [Desktop layout rules here] } /* Mobile/lower-resolution devices */ @media screen and (max-width:480px) { [Mobile layout rules here] } Move the mobile media query block above the desktop media query. By doing this, we’re making sure the cascading effect of CSS is consistent with our mobile first progressive enhancement approach. Reorder media queries so cascade goes from small to large screens Keep basic styles outside of media queries.

Slide 46

Slide 46 text

e absence of support for media queries is in fact the rst media query. —Bryan Rieger, Yiibu

Slide 47

Slide 47 text

Oh come on IE. No love? Because IE 8 and below don’t support media queries, IE isn’t getting the CSS rules that create columns.

Slide 48

Slide 48 text

The conditional comment repeats the line above it ensuring desktop IE sees our layout.css file. IE conditional comments

Slide 49

Slide 49 text

The conditional comment repeats the line above it ensuring desktop IE sees our layout.css file. IE conditional comments or use Respond.js (a media query poly ll for IE)

Slide 50

Slide 50 text

Put CSS images inside media queries to prevent extra downloads @media screen and (min-width:481px){ .header { background:URL('images/taps.jpg') repeat-x; height: 300px; } } Doesn’t work in all browsers. Duplicate downloads are unavoidable if the browser downloads assets if they are inside a media query that doesn’t apply.

Slide 51

Slide 51 text

What to do about the map?

Slide 52

Slide 52 text

What to do about the map? Extremely long URL abbreviated This single iframe causes 47 files to be downloaded! Look inside ontap.html to find this code.

Slide 53

Slide 53 text

What to do about the map? var breakpoint = 481, id = 'mapcontainer', viewportWidth = window.innerWidth; if (viewportWidth > breakpoint) { var mapElement = document.createElement('iframe'); mapElement.id = 'map'; mapElement.src = 'http://maps.google.com/maps?f=q&s ource=s_q&hl=en&geocode=&q=334+NW+11th+Ave,+Portland,+ OR+97209&aq=&sll=37.0625,-95.677068&sspn=58.164117,80. 332031&vpsrc=0&ie=UTF8&hq=&hnear=334+NW+11th+Ave,+Port land,+Oregon+97209&t=m&ll=45.525472,-122.68218&spn=0.0 1804,0.025749&z=14&output=embed'; document.getElementById(id).insertBefore(mapElement, maplink); }

Slide 54

Slide 54 text

One SRC to rule all images Bensons Bubbler There are 16 beer labels on the On Tap Now page that use an img tag like this one for the Bensons Bubbler. Despite the need for multiple versions of this image depending on the screen size, HTML only allows one value for the src.

Slide 55

Slide 55 text

http://www.flickr.com/photos/wscullin/3770015203 first load... http://www.slideshare.net/yiibu/muddling-through-the-mobile-web

Slide 56

Slide 56 text

http://www.flickr.com/photos/wscullin/3770015203 initial request 1 Hmm, Huston - we have a problem... ...no idea what we can deliver to this client... HTML JavaScript stylesheets images Flash video fonts http://www.slideshare.net/yiibu/muddling-through-the-mobile-web

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

Option 1: Use JavaScript to set screen width in a cookie Information on location of large file in query string Instead of using query string, some use data attributes

Slide 60

Slide 60 text

Option 1: Use JavaScript to set screen width in a cookie (function( win ){ // Get screen width var sw = win.screen.width, doc = win.document, // Name of cookie cookieName = def.cookieName || “rwd-screensize”, // Cookie value cookieValue = def.cookieValue || sw > 500 ? ( sw > 1000 ? “large” : “medium” ) : “small”, // Cookie options cookieAge = def.cookieAge || 30000, cookieDomain = def.cookieDomain, cookiePath = def.cookiePath || “/”, //record width cookie for subsequent loads recordRes = (function(){ var date = new Date(); date.setTime( date.getTime() + cookieAge ); doc.cookie = cookieName + “=” + cookieValue + “;” + “expires=” + date.toGMTString() + “;” + (cookiePath ? “path=” + cookiePath + “;” : “” ) + ( cookieDomain ? “domain=” + cookieDomain + “;” : “”); })(); })(this);

Slide 61

Slide 61 text

Option 1: Use JavaScript to set screen width in a cookie # ---------------------------------------------------------------------- # Responsive Images # ---------------------------------------------------------------------- # Mobile-First images that scale responsively and responsibly # Copyright 2011, Scott Jehl, Filament Group, Inc # MIT License # ----------------------------------------- # Start Responsive Images # ----------------------------------------- RewriteEngine On #large cookie, large image RewriteCond %{HTTP_COOKIE} rwd-screensize=large RewriteCond %{QUERY_STRING} large=([^&]+) RewriteRule (.*\/).*\.(jpe?g|png|gif|webp) $1%1 #medium cookie, medium image RewriteCond %{HTTP_COOKIE} rwd-screensize=medium RewriteCond %{QUERY_STRING} medium=([^&]+) RewriteRule (.*\/).*\.(jpe?g|png|gif|webp) $1%1 #large cookie, medium image RewriteCond %{HTTP_COOKIE} rwd-screensize=medium RewriteCond %{QUERY_STRING} medium=([^&]+) RewriteRule (.*\/).*\.(jpe?g|png|gif|webp) $1%1 # ----------------------------------------- # END Responsive Images # -----------------------------------------

Slide 62

Slide 62 text

Option 2: Use noscript to prevent image from loading ”Koala” File doesn’t download because anything inside noscript tags is not is the DOM if the JavaScript is enabled. $(‘noscript[data-large][data-small]’).each(function(){ var src = screen.width >= 500 ? $(this).data(‘large’) : $(this).data(‘small’); $(‘”’’). insertAfter($(this)); }); Script assumes jQuery is loaded.

Slide 63

Slide 63 text

Option 3: Use Sencha.io SRC or similar solution to resize images Set the first part of the src to http://src.sencha.io/ After the slash add the full URL of the image you want to have resized. Sencha.io SRC will resize the image to fit the size of the device screen. For example, if an iPhone visits the site, the image will be constrained to its screen size of 320 by 480 pixels. Bensons Bubbler Replace with your domain and path to the images.

Slide 64

Slide 64 text

Option 3: Use Sencha.io SRC or similar solution to resize images Option 2: Use noscript to prevent image from loading Option 1: Use JavaScript to set screen width in a cookie Which would you choose?

Slide 65

Slide 65 text

Option 1: Use JavaScript to set screen width in a cookie http://blog.yoav.ws/2011/09/Preloaders-cookies-and-race-conditions

Slide 66

Slide 66 text

Option 1: Use JavaScript to set screen width in a cookie Ugh, unreliable. May not work in future. http://blog.yoav.ws/2011/09/Preloaders-cookies-and-race-conditions

Slide 67

Slide 67 text

Option 2: Use noscript to prevent image from loading ”Koala” File doesn’t download because anything inside noscript tags is not is the DOM if the JavaScript is enabled.

Slide 68

Slide 68 text

Option 2: Use noscript to prevent image from loading ”Koala” File doesn’t download because anything inside noscript tags is not is the DOM if the JavaScript is enabled. Ugh, markup is no longer semantic. We’ve changed HTML to t one and only one solution.

Slide 69

Slide 69 text

Option 3: Use Sencha.io SRC or similar solution to resize images Set the first part of the src to http://src.sencha.io/ After the slash add the full URL of the image you want to have resized. Sencha.io SRC will resize the image to fit the size of the device screen. For example, if an iPhone visits the site, the image will be constrained to its screen size of 320 by 480 pixels. Bensons Bubbler Replace with your domain and path to the images.

Slide 70

Slide 70 text

Option 3: Use Sencha.io SRC or similar solution to resize images Set the first part of the src to http://src.sencha.io/ After the slash add the full URL of the image you want to have resized. Sencha.io SRC will resize the image to fit the size of the device screen. For example, if an iPhone visits the site, the image will be constrained to its screen size of 320 by 480 pixels. Bensons Bubbler Replace with your domain and path to the images. Ugh, we’re routing everything through a third party. And how does Sencha.io know what size image to send anyways?

Slide 71

Slide 71 text

What did the Boston Globe do? http://www.flickr.com/photos/bcymet/3564484236/

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

at’s not going to work.

Slide 74

Slide 74 text

No code changes. Unplug it when something better appears.

Slide 75

Slide 75 text

Doing this work does make a difference.

Slide 76

Slide 76 text

How to handle embedded video? How about content reordering? (Flexbox can’t come soon enough) How can we be smart about image and video resizing and converting? What about third-party widgets that aren’t responsive? How do we integrate with content management systems? Mobile First RWD is Difficult

Slide 77

Slide 77 text

9% 4% 21% 38% 4% 25% Mobile is Larger Same Size Less than 10% Savings 11 to 50% Savings 51% to 100% Savings Greater than 100% Savings Comparison of Mobile & Desktop RWD Views Few Mobile First RWD Sites

Slide 78

Slide 78 text

As long as we’re using Sencha, let’s look at device detection.

Slide 79

Slide 79 text

User agent strings are the third rail of web development http://www.flickr.com/photos/flashflood/5183622956/

Slide 80

Slide 80 text

“Browser sniffing is a stupid, error-prone and reviled practice, ‘backed’ by a decade of failures.” http://www.flickr.com/photos/doug88888/4550561194/

Slide 81

Slide 81 text

“User agent sniffing must die. Nice job advocating this bad practice.” http://www.flickr.com/photos/kayepants/391645870/

Slide 82

Slide 82 text

URLs should go to content, not mobile home.

Slide 83

Slide 83 text

Who can forget pages like this? Sadly, this screenshot was taken this month.

Slide 84

Slide 84 text

http://www.flickr.com/photos/brunauto/5062644167/ Our vision of mobile context is often wrong.

Slide 85

Slide 85 text

80% during misc downtime 76% while waiting in lines 62% while watching TV 69% for point of sale research http://www.flickr.com/photos/missmeng/5327470961

Slide 86

Slide 86 text

TMI: 39% use phone on the toilet.

Slide 87

Slide 87 text

http://www.flickr.com/photos/77799978@N00/5351372848/ ese problems lead many to believe separate sites are evil.

Slide 88

Slide 88 text

Mobile developers weren’t blind to the problems with UAs. http://www.flickr.com/photos/sam-wood/3255460999/

Slide 89

Slide 89 text

We should learn from mobile developers’ experiences before forging our own path. http://www.flickr.com/photos/aftab/4214254065/

Slide 90

Slide 90 text

25 of 30 sites in Alexa Top 30 use device detection Other ve don’t offer mobile sites!

Slide 91

Slide 91 text

Feels odd defending device detection. http://www.flickr.com/photos/dunechaser/2936397690/

Slide 92

Slide 92 text

In my ideal world, device detection is unnecessary. http://www.flickr.com/photos/amaynez/2145618222/

Slide 93

Slide 93 text

And yes, sometimes you feel like you need a shower afterwards. http://www.flickr.com/photos/jstar/195805847/

Slide 94

Slide 94 text

http://www.flickr.com/photos/arguez/2247369944/ But this isn’t religion.

Slide 95

Slide 95 text

ese are simply tools. http://www.flickr.com/photos/bre/552152780/

Slide 96

Slide 96 text

Responsible use of device detection. http://www.flickr.com/photos/seven_pillars/3441833846/

Slide 97

Slide 97 text

[THEMATIC_CONSISTENCY] Ensure that content provided by accessing a URI yields a thematically coherent experience when accessed from different devices. is is a realization of the One Web (see 3.1 One Web) principle, whereby content should be accessible on a range of devices irrespective of differences in presentation capabilities and access mechanism. —W3C Mobile Web Best Practices 1.0

Slide 98

Slide 98 text

Incoming requests are run against a device database Code evaluates which device class the device matches based on device data In this hypothetical example, devices are sorted into one of four groups Desktop-like devices Spiffy, newer smartphones Middle-of-the- road smartphones and advanced feature phones Devices that didn’t meet bottom-line requirements The bar is set here (like in chapter 4) Group into as few device classes as possible

Slide 99

Slide 99 text

* it actually stands for ‘uniform.’ close enough ;-) http://www.flickr.com/photos/laughingsquid/255852260/sizes/o/ Support every URL on no matter what device.

Slide 100

Slide 100 text

Device detection means signing up for an arms race. http://www.flickr.com/photos/kyle_slushey_is_awesome/4370958261/

Slide 101

Slide 101 text

But by not discussing it, we’re missing out on major developments that change access to device databases.

Slide 102

Slide 102 text

http://www.flickr.com/ photos/andresthor/ 3963368371/

Slide 103

Slide 103 text

No content

Slide 104

Slide 104 text

generate nal markup device database cookie HEAVY LIFTING UA string tacit data see Bryan’s Adaptation presentation for greater detail on this technique application logic to determine device screen size to override the odd property based on existing knowledge to identify device and therefore, screen size data images video

Slide 105

Slide 105 text

No matter how you approach it, we have a lot of infrastructure to build http://www.flickr.com/photos/wwworks/2942950081/

Slide 106

Slide 106 text

thank you. http://www.flickr.com/photos/24311566@N07/4697623122/

Slide 107

Slide 107 text

http://www.flickr.com/ photos/troyholden/ 5544097948/

Slide 108

Slide 108 text

No content

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

We’re building the future right now http://www.flickr.com/photos/insightimaging/3545443883/

Slide 111

Slide 111 text

No content

Slide 112

Slide 112 text

http://www.flickr.com/photos/eole/3215868087/

Slide 113

Slide 113 text

Let’s get to work.

Slide 114

Slide 114 text

Jason Grigsby • @grigs • cloudfour.com Slides: bit.ly/grigs-11-18-2011 Thanks to Flickr users generously publishing under creative commons

Slide 115

Slide 115 text

bit.ly/hf-mw

Slide 116

Slide 116 text

Jason Grigsby • @grigs • cloudfour.com Slides: bit.ly/grigs-11-18-2011 Thanks to Flickr users generously publishing under creative commons