109
Warsaw, 6 December, 2010 Wojciech Bednarski http://linkedin.com/in/bednarski http://twitter.com/wbednarski Front end for back end developers 1

Front end for back end developers

Embed Size (px)

DESCRIPTION

Workshop for back end developers at Roche, 6 December 2010.

Citation preview

Page 1: Front end for back end developers

Warsaw, 6 December, 2010

Wojciech Bednarski

http://linkedin.com/in/bednarskihttp://twitter.com/wbednarski

Front end for back end developers

1

Page 2: Front end for back end developers

Browsers

2

Page 3: Front end for back end developers

• WorldWideWeb browser 1991

• Created by Tim Berners-Lee

• Ran on NeXSTEP OS

• Renamed to Nexus

3

Page 4: Front end for back end developers

• Mosaic 1993

• Netscape 1994

• Internet Explorer 1995

• Opera 1996

• Mozilla 1998

4

Page 5: Front end for back end developers

• The end of the First Browsers War, IE domination 2002

5

Page 6: Front end for back end developers

• Apple Safari 2003

• Firefox 2004

• Internet Explorer 7 2006

• Google Chrome 2008

• Internet Explorer 8 2009

6

Page 8: Front end for back end developers

• Google Chrome

• Mozilla Firefox

• Safari

• Opera

• Internet Explorer

Web Browsers

8

Page 9: Front end for back end developers

• Safari

• Android Browser

• BlackBerry Browser

• Opera Mobile

Mobile Browsers

9

Page 10: Front end for back end developers

• Trident is developed by Microsoft (Windows)

• Tasman was developed by Microsoft (Macintosh)

• Gecko is developed by the Mozilla Foundation

• KHTML is developed by the KDE project

• WebKit is a fork of KHTML by Apple Inc.

Browsers engines

10

Page 11: Front end for back end developers

Internet Explorer:Mac

11

Page 12: Front end for back end developers

12

Page 13: Front end for back end developers

IE:Mac - The Innovation• Support for annotative glosses to

Japanese kanji and Chinese characters

• Scrapbook feature lets the user archive any page in its current state.

• Auction Manager feature automatically tracks eBay auctions.

• Robust PNG support (version 5.0)

• An option to change the browser color, to match the colors of the iMac G3. The first builds had a choice of 9 colors, but later builds had 15.

• Print Preview functionality allowing for adjustment of the font-size from within the preview pane.

• Page Holder sidebar functionality allowing users to hold a page in the sidebar

13

Page 14: Front end for back end developers

Behind the scene

14

Page 15: Front end for back end developers

Browsers architecture

15

Page 16: Front end for back end developers

• The user interface - this includes the address bar, back/forward button, bookmarking menu etc. Every part of the browser display except the main window where you see the requested page.

• The browser engine - the interface for querying and manipulating the rendering engine.

• The rendering engine - responsible for displaying the requested content. For example if the requested content is HTML, it is responsible for parsing the HTML and CSS and displaying the parsed content on the screen.

• Networking - used for network calls, like HTTP requests. It has platform independent interface and underneath implementations for each platform.

• UI backend - used for drawing basic widgets like combo boxes and windows.

• JavaScript interpreter. Used to parse and execute the JavaScript code.

• Data storage. This is a persistence layer. The browser needs to save all sorts of data on the hard disk, for examples, cookies, HTML5 web database.

16

Page 17: Front end for back end developers

Parsing HTML• Document Object Model is the object

presentation of the HTML document and the interface of HTML elements to the outside world

<html> <body> <p> Hello World </p> <div> <img src="example.png" alt="" /></div> </body></html>

17

Page 19: Front end for back end developers

JavaScript parsing

• The model of the web is synchronous. Authors expect scripts to be parsed and executed immediately when the parser reaches a <script> tag. The parsing of the document halts until the script was executed. If the script is external then the resource must be first fetched from the network - this is also done synchronously, the parsing halts until the resource is fetched. This was the model for many years and is also specified in HTML 4 and 5 specifications.

• http://www.whatbrowser.org/en/more/

19

Page 20: Front end for back end developers

Browsers error tolerance

• You never get an "Invalid Syntax" error on an page served as text/html. Browsers fix an invalid content and go on.

20

Page 21: Front end for back end developers

HTML

21

Page 22: Front end for back end developers

• HTML 2.0 1995

• HTML 3.2 1997

• HTML 4.0 1997

• HTML 4.01 1999

22

Page 23: Front end for back end developers

• XHTML 1.0 2000

• XHTML 1.1 2001

• XHTML 2

23

Page 24: Front end for back end developers

• HTML 5

• WHATWG 2004

• W3C 2007

24

Page 25: Front end for back end developers

Simple solutions are preferred to complex ones, when possible.

HTML 5

25

Page 26: Front end for back end developers

• HTML 4.01 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

• XHTML 1.0 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

• HTML 5 <!DOCTYPE html>

26

Page 27: Front end for back end developers

• HTML 4.01 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

• XHTML 1.0 <?xml version="1.0" encoding="UTF-8"?> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

• HTML 5 <meta charset="utf-8">

27

Page 28: Front end for back end developers

• XHTML 1.0 and previous <link rel="stylesheet" type="text/css" href="file.css"> <script type="text/javascript"></script>

• HTML 5 <link rel="stylesheet" href="file.css"> <script></script>

28

Page 29: Front end for back end developers

• <header>

• <nav>

• <section>

• <article>

• <aside>

• <footer>

• <details>

• <summary>

• <command>

• <menu>

• <canvas>

• <audio>

• <video>

29

Page 30: Front end for back end developers

• input type="number"

• input type="search"

• input type="range"

• input type="email"

• input type="date"

• input type="url"

• input type="number"

• input type="search"

• input type="color"

• input type="tel"

30

Page 31: Front end for back end developers

• role=”button”

• role=”navigation”

• role=”progressbar”

• role=”button”

• role=”link”

• role-”cell”

31

Page 32: Front end for back end developers

A basic HTML5 document

<!DOCTYPE html><html> <head> <title>Sample page</title> </head> <body> <h1>Sample page</h1> <p>This is a <a href="demo.html">simple</a> sample.</p> <!-- this is a comment --> </body></html>

32

Page 33: Front end for back end developers

DOM treeDOCTYPE: html

htmlhead

#text: ⏎␣␣title

#text: Sample page#text: ⏎␣

#text: ⏎␣body

#text: ⏎␣␣h1

#text: Sample page#text: ⏎␣␣p

#text: This is aa href="demo.html"#text: simple

#text: sample.#text: ⏎␣␣#comment: this is a comment#text: ⏎␣⏎

33

Page 34: Front end for back end developers

Demo!

• http://slides.html5rocks.com/#slide24

34

Page 35: Front end for back end developers

Future read

• http://diveintohtml5.org/

• http://www.whatwg.org/specs/web-apps/current-work/

35

Page 36: Front end for back end developers

CSS

36

Page 37: Front end for back end developers

Selector

• p { color: red }

37

Page 38: Front end for back end developers

Declaration block

• p { color: red }

38

Page 39: Front end for back end developers

Declaration

• p { color: red }

39

Page 40: Front end for back end developers

Property

• p { color: red }

40

Page 41: Front end for back end developers

Value

• p { color: red }

41

Page 42: Front end for back end developers

Inheritance

• <p>Because <strong>strong element</strong> inherited the color property from the p element, it is displayed in red color.</p>

42

Page 43: Front end for back end developers

Not all properties are inherited

43

Page 44: Front end for back end developers

Demo!

• http://slides.html5rocks.com/#slide39

44

Page 45: Front end for back end developers

45

Page 46: Front end for back end developers

JavaScript

46

Page 47: Front end for back end developers

JavaScript

47

Page 48: Front end for back end developers

JavaScript

• JavaScript is an implementation of the ECMAScript language standard and is typically used to enable programmatic access to computational objects within a host environment. It can be characterized as a prototype-based object-oriented scripting language that is dynamic, weakly typed and has first-class functions. It is also considered a functional programming language like Scheme and OCaml because it has closures and supports higher-order functions.

48

Page 49: Front end for back end developers

Doug Crockford

• "JavaScript doesn't suck. You're just doing it wrong."

49

Page 50: Front end for back end developers

50

Page 51: Front end for back end developers

Detailed Results Average (ms)IE8 3746

Firefox 3.6.12 753Safari 5.0.2 328

irefox 4.0 Pre-Release Beta7

277Chrome 7.0 262Opera 10.63 246

Opera 11 Alpha 242Chrome 8.0 Beta 233

IE9 Platform Preview #7 216

51

Page 52: Front end for back end developers

// Variable declarationvar firstName = "Roche";

// Function declarationfunction saying () { return "Hello Roche!";}

52

Page 53: Front end for back end developers

var expr, fruit;switch (expr) { case "Oranges": fruit = "Orange"; break;

case "Apples": fruit = "Orange"; break;}

53

Page 54: Front end for back end developers

/ Object literalvar apple = { firstName : "Apple"};

// Array literalvar fruits = ["Apple", "Orange"];

54

Page 55: Front end for back end developers

// Shorthand assignmentfunction (fruit) { var life = fruit || "Apple";}

// Ternary operators(fruit) ? "Apple" : "Window";

55

Page 56: Front end for back end developers

// Short-circuit logicif (obj && obj.property) { obj.property = "Fiery Red";}

56

Page 57: Front end for back end developers

• string

• number

• boolean

• null

• undefined

• object

57

Page 58: Front end for back end developers

// Variable declarationvar firstName = "Apple";

// Function declarationfunction saying () { return "Hello Apple!";}

typeof firstName // stringtypeof saying // function

58

Page 59: Front end for back end developers

// Object declarationvar apple = { firstName : "Apple"};

// Array declarationvar fruits = ["Apple", "Orange"];

typeof apple // objecttypeof fruits // object

59

Page 60: Front end for back end developers

// Object declarationvar apple = { firstName : "Apple"};

// Array declarationvar fruits = ["Apple", "Orange"];

apple instanceof Array // falsefruits instanceof Array // true

60

Page 61: Front end for back end developers

// Various "false" valuesvar nullVal = null;var undefinedVal = undefined;var zeroVal = 0;var falseVal = false;var emptyString = "";

// All would equal false in an if-clauseif (emptyString) { // Would never go in here}

61

Page 62: Front end for back end developers

// Equalityif (7 == "7") { // true}

// Identityif (7 === "7") { // false}

62

Page 63: Front end for back end developers

// Type coercionvar sum = "5" + 6 + 7; // 567

63

Page 64: Front end for back end developers

// Prevent type coercionvar sum = parseInt("5", 10) + 6 + 7; // 18

64

Page 65: Front end for back end developers

// Using the arguments collectionfunction fruits () { var allFruits = []; for (var i=0, il=arguments.length; i<il; i++) { allFruits.push(arguments[i]); return allFruits.join(" & "); }}

// Apple and Orangefruits("Apple", "Orange");

// Applefriends("Apple");

65

Page 66: Front end for back end developers

// Object declarationfunction Fruit () { this.name = "Apple"; this.color = "Green";}

var fruit = new Fruit();

66

Page 67: Front end for back end developers

// Object declaration, literal stylevar fruit = { name : "Apple", color : "green"};

67

Page 68: Front end for back end developers

// Iterating over propertiesfor (var item in fruit) { console.log(item + ": " + fruit[item]);}

68

Page 69: Front end for back end developers

// Object declarationvar fruit = { name : "Apple"};

// Safe check for propertyif ("name" in fruit) { console.log(fruit.name);}

69

Page 70: Front end for back end developers

// Object declarationfunction Apple { this.color = "Green";};

// Method set via prototypeApple.prototype.green = function () { return true;};

70

Page 71: Front end for back end developers

// Scope - global or local

// Globalvar quote = "Stay Hungry. Stay Foolish."

function () { // Local var green = "Apple";

// Global wojtek = "Fiery Red";}

71

Page 72: Front end for back end developers

Tools

72

Page 73: Front end for back end developers

Firebug

73

Page 74: Front end for back end developers

• Inspect and edit HTML

• Edit CSS

• Visualize CSS metrics

• Monitor network activity

• Debug and profile JavaScript

• Show errors

• Explore the DOM

• Execute JavaScript on the fly

• Logging for JavaScript

Firebug

74

Page 75: Front end for back end developers

YSlow

75

Page 76: Front end for back end developers

YSlow• Make fewer HTTP requests

• Use a Content Delivery Network (CDN)

• Avoid empty src or href

• Add Expires headers

• Compress components with gzip

• Put CSS at top

• Put JavaScript at bottom

• Avoid CSS expressions

• Make JavaScript and CSS external

• Reduce DNS lookups

• Minify JavaScript and CSS

• Avoid URL redirects

• Remove duplicate JavaScript and CSS

76

Page 77: Front end for back end developers

YSlow• Configure entity tags (ETags)

• Make AJAX cacheable

• Use GET for AJAX requests

• Reduce the number of DOM elements

• Avoid HTTP 404 (Not Found) error

• Reduce cookie size

• Use cookie-free domains

• Avoid AlphaImageLoader filter

• Do not scale images in HTML

• Make favicon small and cacheable

77

Page 78: Front end for back end developers

Page Speed by Google

78

Page 79: Front end for back end developers

Page Speed• Optimizing caching — keeping your application's data and logic off the

network altogether

• Minimizing round-trip times — reducing the number of serial request-response cycles

• Minimizing request overhead — reducing upload size

• Minimizing payload size — reducing the size of responses, downloads, and cached pages

• Optimizing browser rendering — improving the browser's layout of a page

79

Page 80: Front end for back end developers

Web Developer

80

Page 81: Front end for back end developers

Web Developer

• The Web Developer extension adds various web developer tools to a browser. The extension is available for Firefox and Chrome, and will run on any platform that these browsers support including Windows, Mac OS X and Linux.

81

Page 82: Front end for back end developers

Validators

• http://validator.w3.org/

• http://jigsaw.w3.org/css-validator/

• http://www.jslint.com/

82

Page 83: Front end for back end developers

Performance

83

Page 84: Front end for back end developers

Performance

• Psychology - effect of waiting - make people happy

• Optimization

84

Page 85: Front end for back end developers

Minimize HTTP Requests

• 80% of the end-user response time is spent on the front-end. Most of this time is tied up in downloading all the components in the page: images, stylesheets, scripts, Flash, etc. Reducing the number of components in turn reduces the number of HTTP requests required to render the page. This is the key to faster pages.

85

Page 86: Front end for back end developers

Use a Content Delivery Network

• Deploying your content across multiple, geographically dispersed servers will make your pages load faster from the user's perspective.

86

Page 87: Front end for back end developers

Add an Expires or a Cache-Control Header

• For static components: implement "Never expire" policy by setting far future Expires header

• For dynamic components: use an appropriate Cache-Control header to help the browser with conditional requests

87

Page 88: Front end for back end developers

Gzip Components

• The time it takes to transfer an HTTP request and response across the network can be significantly reduced by decisions made by front-end engineers.

88

Page 89: Front end for back end developers

Put StyleSheets at the Top

• Putting stylesheets in the HEAD allows the page to render progressively.

89

Page 90: Front end for back end developers

Put JavaScript at the Bottom

• The problem caused by scripts is that they block parallel downloads. The HTTP/1.1 specification suggests that browsers download no more than two components in parallel per hostname.

90

Page 91: Front end for back end developers

Avoid CSS Expressions

• CSS expressions are a powerful (and dangerous) way to set CSS properties dynamically.

91

Page 92: Front end for back end developers

Make JavaScript and CSS External

• Using external files in the real world generally produces faster pages because the JavaScript and CSS files are cached by the browser. JavaScript and CSS that are inlined in HTML documents get downloaded every time the HTML document is requested.

92

Page 93: Front end for back end developers

Reduce DNS Lookups

• DNS has a cost. It typically takes 20-120 milliseconds for DNS to lookup the IP address for a given hostname.

93

Page 94: Front end for back end developers

Avoid Redirects

• It hurts performance to include the same JavaScript file twice in one page.

94

Page 95: Front end for back end developers

Configure ETags

• An ETag is a string that uniquely identifies a specific version of a component.

HTTP/1.1 200 OK Last-Modified: Tue, 12 Dec 2006 03:03:59 GMT ETag: "10c24bc-4ab-457e1c1f" Content-Length: 12195

95

Page 96: Front end for back end developers

Make AJAX Cacheable

• When users request a page, it can take anywhere from 200 to 500ms for the backend server to stitch together the HTML page. During this time, the browser is idle as it waits for the data to arrive. In PHP you have the function flush(). It allows you to send your partially ready HTML response to the browser so that the browser can start fetching components while your backend is busy with the rest of the HTML page.

96

Page 97: Front end for back end developers

Use GET for AJAX Requests

• POST is implemented in the browsers as a two-step process: sending the headers first, then sending data. So it's best to use GET, which only takes one TCP packet to send (unless you have a lot of cookies). The maximum URL length in IE is 2K.

97

Page 98: Front end for back end developers

Reduce the Number of DOM Elements

98

Page 99: Front end for back end developers

No 404s

99

Page 100: Front end for back end developers

Reduce Cookie Size

100

Page 101: Front end for back end developers

Use Cookie-Free Domains for Components

101

Page 102: Front end for back end developers

Avoid Filters

102

Page 103: Front end for back end developers

Do Not Scale Images in HTML

103

Page 104: Front end for back end developers

Make favicon.ico Small and Cacheable

104

Page 105: Front end for back end developers

Usability

105

Page 106: Front end for back end developers

Web standards checklist

• http://www.maxdesign.com.au/articles/checklist/

106

Page 107: Front end for back end developers

Workshop -> Now!

107