Share
Embed
Transcript
341 slides
-
Introduction to Prototype Christophe Porteneuve The Ajax Experience · San
Francisco, July 25 2007 -
“Prototype,” uh? • Open-source JS framework • Extracted from real
apps, real needs -
“Prototype,” uh? • Open-source JS framework • Extracted from real
apps, real needs • Makes JS feel nicer (e.g. more expressive) -
“Prototype,” uh? • Open-source JS framework • Extracted from real
apps, real needs • Makes JS feel nicer (e.g. more expressive) • NOT widget-oriented. More of a core... -
“Prototype,” uh? • Open-source JS framework • Extracted from real
apps, real needs • Makes JS feel nicer (e.g. more expressive) • NOT widget-oriented. More of a core... • Actively maintained -
“Prototype,” uh? • Open-source JS framework • Extracted from real
apps, real needs • Makes JS feel nicer (e.g. more expressive) • NOT widget-oriented. More of a core... • Actively maintained • Excellent online documentation -
“Porteneuve,” uh? • 29, lives in Paris. Web work since
1995. • CTO @ Ciblo.net -
“Porteneuve,” uh? • 29, lives in Paris. Web work since
1995. • CTO @ Ciblo.net • Prototype Core member -
“Porteneuve,” uh? • 29, lives in Paris. Web work since
1995. • CTO @ Ciblo.net • Prototype Core member • Rails & script.aculo.us contributor -
So you said, “intro...” • We’ll take it easy: newbies,
welcome! • There’s an advanced session anyway -
So you said, “intro...” • We’ll take it easy: newbies,
welcome! • There’s an advanced session anyway • What’s in this one? -
So you said, “intro...” • We’ll take it easy: newbies,
welcome! • There’s an advanced session anyway • What’s in this one? 1. These JS things you should know -
So you said, “intro...” • We’ll take it easy: newbies,
welcome! • There’s an advanced session anyway • What’s in this one? 1. These JS things you should know 2. How Prototype extends JavaScript... -
So you said, “intro...” • We’ll take it easy: newbies,
welcome! • There’s an advanced session anyway • What’s in this one? 1. These JS things you should know 2. How Prototype extends JavaScript... 3. How it puts the DOM on steroids... -
So you said, “intro...” • We’ll take it easy: newbies,
welcome! • There’s an advanced session anyway • What’s in this one? 1. These JS things you should know 2. How Prototype extends JavaScript... 3. How it puts the DOM on steroids... 4. And how it makes AJAX a snap! -
Everything is an object var speaker = { name: 'Christophe',
age: 29, location: 'Paris' }; -
Everything is an object var speaker = { name: 'Christophe',
age: 29, location: 'Paris' }; person['name'] // 'Christophe' -
Everything is an object var speaker = { name: 'Christophe',
age: 29, location: 'Paris' }; person['name'] // 'Christophe' person['name'] = 'Mislav'; -
Everything is an object var speaker = { name: 'Christophe',
age: 29, location: 'Paris' }; person['name'] // 'Christophe' person['name'] = 'Mislav'; person['name'] // 'Mislav' -
Everything is an object var speaker = { name: 'Christophe',
age: 29, location: 'Paris' }; person['name'] // 'Christophe' person['name'] = 'Mislav'; person['name'] // 'Mislav' person.name // 'Mislav' -
Everything is an object var speaker = { name: 'Christophe',
age: 29, location: 'Paris' }; person['name'] // 'Christophe' person['name'] = 'Mislav'; person['name'] // 'Mislav' person.name // 'Mislav' person.name = 'Tobie'; -
Everything is an object var speaker = { name: 'Christophe',
age: 29, location: 'Paris' }; person['name'] // 'Christophe' person['name'] = 'Mislav'; person['name'] // 'Mislav' person.name // 'Mislav' person.name = 'Tobie'; person.name // 'Tobie' -
Arrays are not associative var data = ['mark', 'john', 'susie'];
for (var name in data) alert(name); -
Arrays are not associative • for...in iterates over all the
properties! var data = ['mark', 'john', 'susie']; for (var name in data) alert(name); ✘ -
Arrays are not associative • for...in iterates over all the
properties! ‣ Includes all of Prototype’s extensions! var data = ['mark', 'john', 'susie']; for (var name in data) alert(name); ✘ -
Arrays are not associative • for...in iterates over all the
properties! ‣ Includes all of Prototype’s extensions! var data = ['mark', 'john', 'susie']; for (var name in data) alert(name); ✘ var data = ['mark', 'john', 'susie']; -
Arrays are not associative • for...in iterates over all the
properties! ‣ Includes all of Prototype’s extensions! var data = ['mark', 'john', 'susie']; for (var name in data) alert(name); ✘ var data = ['mark', 'john', 'susie']; for (var index = 0, l = data.length; index < l; ++index) alert(data[index]); -
Arrays are not associative • for...in iterates over all the
properties! ‣ Includes all of Prototype’s extensions! var data = ['mark', 'john', 'susie']; for (var name in data) alert(name); ✘ var data = ['mark', 'john', 'susie']; for (var index = 0, l = data.length; index < l; ++index) alert(data[index]); data.each(alert); -
Functions are objects, too. var personToString = function(person) { return
person.name + ', ' + person.age + ' (' + person.location + ')'; } They’re values like any other: -
Functions are objects, too. var personToString = function(person) { return
person.name + ', ' + person.age + ' (' + person.location + ')'; } var speaker = { name: 'Christophe', age: 29, location: 'Paris' }; speaker.toString = function() { return this.name + ', ' + this.age + ' (' + this.location + ')'; }; They’re values like any other: Same goes for methods: -
Functions as constructors function Person(name, age, location) { this.name =
name; this.age = age; this.location = location; this.toString = function() { /*...*/ } } Just initialize your fields using this: -
Functions as constructors function Person(name, age, location) { this.name =
name; this.age = age; this.location = location; this.toString = function() { /*...*/ } } var speaker = new Person('Christophe', 29, 'Paris'); speaker.name // 'Christophe' speaker.toString() // 'Christophe, 29 (Paris)' Just initialize your fields using this: And invoke using new: -
The prototype property • this.method = ... quickly get tedious
‣ The constructors get overweight -
The prototype property • this.method = ... quickly get tedious
‣ The constructors get overweight ‣ It’s all actually singleton methods! -
The prototype property • this.method = ... quickly get tedious
‣ The constructors get overweight ‣ It’s all actually singleton methods! ‣ Save it for private/privileged methods -
The prototype property • this.method = ... quickly get tedious
‣ The constructors get overweight ‣ It’s all actually singleton methods! ‣ Save it for private/privileged methods • prototype: all instances rely on it -
The prototype property • this.method = ... quickly get tedious
‣ The constructors get overweight ‣ It’s all actually singleton methods! ‣ Save it for private/privileged methods • prototype: all instances rely on it ‣ Dynamic lookup: not just later instantiations! -
The prototype property • this.method = ... quickly get tedious
‣ The constructors get overweight ‣ It’s all actually singleton methods! ‣ Save it for private/privileged methods • prototype: all instances rely on it ‣ Dynamic lookup: not just later instantiations! ‣ The class’ repository of public methods -
The prototype property function Person(name, age, location) { ... }
var sam = new Person('Sam', 23, 'Chicago'); -
The prototype property function Person(name, age, location) { ... }
var sam = new Person('Sam', 23, 'Chicago'); Person.prototype.toString = function() { return this.name + ', ' + this.age + ' (' + this.location + ')'; } -
The prototype property function Person(name, age, location) { ... }
var sam = new Person('Sam', 23, 'Chicago'); Person.prototype.toString = function() { return this.name + ', ' + this.age + ' (' + this.location + ')'; } var tdd = new Person('Christophe', 29, 'Paris'); -
The prototype property function Person(name, age, location) { ... }
var sam = new Person('Sam', 23, 'Chicago'); Person.prototype.toString = function() { return this.name + ', ' + this.age + ' (' + this.location + ')'; } var tdd = new Person('Christophe', 29, 'Paris'); tdd.toString() // 'Christophe, 29 (Paris)' -
The prototype property function Person(name, age, location) { ... }
var sam = new Person('Sam', 23, 'Chicago'); Person.prototype.toString = function() { return this.name + ', ' + this.age + ' (' + this.location + ')'; } var tdd = new Person('Christophe', 29, 'Paris'); tdd.toString() // 'Christophe, 29 (Paris)' sam.toString() // 'Sam, 23 (Chicago)' -
What could be improved? • So much stuff (no offense,
Brendan!) • Arrays, collections in general, iterators... -
What could be improved? • So much stuff (no offense,
Brendan!) • Arrays, collections in general, iterators... • Closures -
What could be improved? • So much stuff (no offense,
Brendan!) • Arrays, collections in general, iterators... • Closures • Comprehensions -
What could be improved? • So much stuff (no offense,
Brendan!) • Arrays, collections in general, iterators... • Closures • Comprehensions • Dynamic “inheritance,” method overriding -
What could be improved? • So much stuff (no offense,
Brendan!) • Arrays, collections in general, iterators... • Closures • Comprehensions • Dynamic “inheritance,” method overriding • ...and more! -
What do we improve? • We beef up the basics:
Array, String, Number, Function... -
What do we improve? • We beef up the basics:
Array, String, Number, Function... • We unify event handling -
What do we improve? • We beef up the basics:
Array, String, Number, Function... • We unify event handling • Sure, JS 1.7-2.0 introduce a ton of cool -
What do we improve? • We beef up the basics:
Array, String, Number, Function... • We unify event handling • Sure, JS 1.7-2.0 introduce a ton of cool ‣ We try not to trump native stuff... -
What do we improve? • We beef up the basics:
Array, String, Number, Function... • We unify event handling • Sure, JS 1.7-2.0 introduce a ton of cool ‣ We try not to trump native stuff... ‣ ...but not everybody has Firefox 3! -
What do we improve? • We beef up the basics:
Array, String, Number, Function... • We unify event handling • Sure, JS 1.7-2.0 introduce a ton of cool ‣ We try not to trump native stuff... ‣ ...but not everybody has Firefox 3! ‣ ...and the syntax doesn’t always honor POLS ;) -
Stretching your Strings // 29 new methods. Here are just
a few... 'Prototype'.include('to') // true -
Stretching your Strings // 29 new methods. Here are just
a few... 'Prototype'.include('to') // true 'Prototype'.startsWith('Active') // false -
Stretching your Strings // 29 new methods. Here are just
a few... 'Prototype'.include('to') // true 'Prototype'.startsWith('Active') // false 'Prototype'.endsWith('type') // true -
Stretching your Strings // 29 new methods. Here are just
a few... 'Prototype'.include('to') // true 'Prototype'.startsWith('Active') // false 'Prototype'.endsWith('type') // true 'Ho! '.times(3).strip() // 'Ho! Ho! Ho!' -
Stretching your Strings // 29 new methods. Here are just
a few... 'Prototype'.include('to') // true 'Prototype'.startsWith('Active') // false 'Prototype'.endsWith('type') // true 'Ho! '.times(3).strip() // 'Ho! Ho! Ho!' 'x > 0 && x < 100'.escapeHTML() // 'x > 0 && x < 100' -
Stretching your Strings // 29 new methods. Here are just
a few... 'Prototype'.include('to') // true 'Prototype'.startsWith('Active') // false 'Prototype'.endsWith('type') // true 'Ho! '.times(3).strip() // 'Ho! Ho! Ho!' 'x > 0 && x < 100'.escapeHTML() // 'x > 0 && x < 100' 'Extending JavaScript'.gsub(/([aeiouy])([^aeiouy]{2})/i, '#{2}#{1}') -
Stretching your Strings // 29 new methods. Here are just
a few... 'Prototype'.include('to') // true 'Prototype'.startsWith('Active') // false 'Prototype'.endsWith('type') // true 'Ho! '.times(3).strip() // 'Ho! Ho! Ho!' 'x > 0 && x < 100'.escapeHTML() // 'x > 0 && x < 100' 'Extending JavaScript'.gsub(/([aeiouy])([^aeiouy]{2})/i, '#{2}#{1}') // 'xtEndengi JavScarpti' -
Stretching your Strings // 29 new methods. Here are just
a few... 'Prototype'.include('to') // true 'Prototype'.startsWith('Active') // false 'Prototype'.endsWith('type') // true 'Ho! '.times(3).strip() // 'Ho! Ho! Ho!' 'x > 0 && x < 100'.escapeHTML() // 'x > 0 && x < 100' 'Extending JavaScript'.gsub(/([aeiouy])([^aeiouy]{2})/i, '#{2}#{1}') // 'xtEndengi JavScarpti' 'the ajax experience'.gsub(/\w+/, function(match) { return match[0].capitalize(); }) -
Stretching your Strings // 29 new methods. Here are just
a few... 'Prototype'.include('to') // true 'Prototype'.startsWith('Active') // false 'Prototype'.endsWith('type') // true 'Ho! '.times(3).strip() // 'Ho! Ho! Ho!' 'x > 0 && x < 100'.escapeHTML() // 'x > 0 && x < 100' 'Extending JavaScript'.gsub(/([aeiouy])([^aeiouy]{2})/i, '#{2}#{1}') // 'xtEndengi JavScarpti' 'the ajax experience'.gsub(/\w+/, function(match) { return match[0].capitalize(); }) // 'The Ajax Experience' -
Arrays as über-collections // 34 new methods, 19 of which
from Enumerable (4 slides away)... -
Arrays as über-collections // 34 new methods, 19 of which
from Enumerable (4 slides away)... [1, 2, 3].first() // 3 -
Arrays as über-collections // 34 new methods, 19 of which
from Enumerable (4 slides away)... [1, 2, 3].first() // 3 [4, 5, 6].last() // 6 -
Arrays as über-collections // 34 new methods, 19 of which
from Enumerable (4 slides away)... [1, 2, 3].first() // 3 [4, 5, 6].last() // 6 [7, 8, 9].indexOf(8) // 1 -
Arrays as über-collections // 34 new methods, 19 of which
from Enumerable (4 slides away)... [1, 2, 3].first() // 3 [4, 5, 6].last() // 6 [7, 8, 9].indexOf(8) // 1 [1, [2, 3], [4, [5], []]].flatten() // [1, 2, 3, 4, 5] -
Arrays as über-collections // 34 new methods, 19 of which
from Enumerable (4 slides away)... [1, 2, 3].first() // 3 [4, 5, 6].last() // 6 [7, 8, 9].indexOf(8) // 1 [1, [2, 3], [4, [5], []]].flatten() // [1, 2, 3, 4, 5] [1, null, , 2, null, 3].compact() // [1, 2, 3] -
Arrays as über-collections // 34 new methods, 19 of which
from Enumerable (4 slides away)... [1, 2, 3].first() // 3 [4, 5, 6].last() // 6 [7, 8, 9].indexOf(8) // 1 [1, [2, 3], [4, [5], []]].flatten() // [1, 2, 3, 4, 5] [1, null, , 2, null, 3].compact() // [1, 2, 3] [1, 2, 3, 2, 1].without(1) // [2, 3, 2] -
Arrays as über-collections // 34 new methods, 19 of which
from Enumerable (4 slides away)... [1, 2, 3].first() // 3 [4, 5, 6].last() // 6 [7, 8, 9].indexOf(8) // 1 [1, [2, 3], [4, [5], []]].flatten() // [1, 2, 3, 4, 5] [1, null, , 2, null, 3].compact() // [1, 2, 3] [1, 2, 3, 2, 1].without(1) // [2, 3, 2] [1, 2, 3, 3, 4, 2, 5].uniq() // [1, 2, 3, 4, 5] -
Arrays as über-collections // 34 new methods, 19 of which
from Enumerable (4 slides away)... [1, 2, 3].first() // 3 [4, 5, 6].last() // 6 [7, 8, 9].indexOf(8) // 1 [1, [2, 3], [4, [5], []]].flatten() // [1, 2, 3, 4, 5] [1, null, , 2, null, 3].compact() // [1, 2, 3] [1, 2, 3, 2, 1].without(1) // [2, 3, 2] [1, 2, 3, 3, 4, 2, 5].uniq() // [1, 2, 3, 4, 5] function greet() { return 'Hey ' + $A(arguments).first() + '!'; } -
Arrays as über-collections // 34 new methods, 19 of which
from Enumerable (4 slides away)... [1, 2, 3].first() // 3 [4, 5, 6].last() // 6 [7, 8, 9].indexOf(8) // 1 [1, [2, 3], [4, [5], []]].flatten() // [1, 2, 3, 4, 5] [1, null, , 2, null, 3].compact() // [1, 2, 3] [1, 2, 3, 2, 1].without(1) // [2, 3, 2] [1, 2, 3, 3, 4, 2, 5].uniq() // [1, 2, 3, 4, 5] function greet() { return 'Hey ' + $A(arguments).first() + '!'; } greet('Sam', 'Thomas', 'John') // 'Hey Sam!' -
Arrays as über-collections // 34 new methods, 19 of which
from Enumerable (4 slides away)... [1, 2, 3].first() // 3 [4, 5, 6].last() // 6 [7, 8, 9].indexOf(8) // 1 [1, [2, 3], [4, [5], []]].flatten() // [1, 2, 3, 4, 5] [1, null, , 2, null, 3].compact() // [1, 2, 3] [1, 2, 3, 2, 1].without(1) // [2, 3, 2] [1, 2, 3, 3, 4, 2, 5].uniq() // [1, 2, 3, 4, 5] function greet() { return 'Hey ' + $A(arguments).first() + '!'; } greet('Sam', 'Thomas', 'John') // 'Hey Sam!' $w('eenie meenie minie moe') // ['eenie', 'meenie', 'minie', 'moe'] -
Numbers, the Ruby way (5).times(function() { // Processed 5 times
}) 'hello world!'.length.toPaddedString(3, 16) // '00c' -
Numbers, the Ruby way (5).times(function() { // Processed 5 times
}) 'hello world!'.length.toPaddedString(3, 16) // '00c' (221).toColorPart() // 'dd' (2-digit hex) -
Numbers, the Ruby way (5).times(function() { // Processed 5 times
}) 'hello world!'.length.toPaddedString(3, 16) // '00c' (221).toColorPart() // 'dd' (2-digit hex) Math.PI.ceil() // 4 -
Numbers, the Ruby way (5).times(function() { // Processed 5 times
}) 'hello world!'.length.toPaddedString(3, 16) // '00c' (221).toColorPart() // 'dd' (2-digit hex) Math.PI.ceil() // 4 (3 - 2 * 4).abs() // 5 -
A few Function tricks function add(x, y) { return x
+ y; } function mul(x, y) { return x * y; } -
A few Function tricks function add(x, y) { return x
+ y; } function mul(x, y) { return x * y; } var add4 = add.curry(4); -
A few Function tricks function add(x, y) { return x
+ y; } function mul(x, y) { return x * y; } var add4 = add.curry(4); var mul5 = mul.curry(5); -
A few Function tricks function add(x, y) { return x
+ y; } function mul(x, y) { return x * y; } var add4 = add.curry(4); var mul5 = mul.curry(5); add4(3) // 7 -
A few Function tricks function add(x, y) { return x
+ y; } function mul(x, y) { return x * y; } var add4 = add.curry(4); var mul5 = mul.curry(5); add4(3) // 7 mul5(3) // 15 -
A few Function tricks function add(x, y) { return x
+ y; } function mul(x, y) { return x * y; } var add4 = add.curry(4); var mul5 = mul.curry(5); add4(3) // 7 mul5(3) // 15 function greet(name) { alert('Hey ' + name + '!'); } -
A few Function tricks function add(x, y) { return x
+ y; } function mul(x, y) { return x * y; } var add4 = add.curry(4); var mul5 = mul.curry(5); add4(3) // 7 mul5(3) // 15 function greet(name) { alert('Hey ' + name + '!'); } greet.delay(1, 'Mark'); // will greet a second later -
A few Function tricks function add(x, y) { return x
+ y; } function mul(x, y) { return x * y; } var add4 = add.curry(4); var mul5 = mul.curry(5); add4(3) // 7 mul5(3) // 15 function greet(name) { alert('Hey ' + name + '!'); } greet.delay(1, 'Mark'); // will greet a second later updateDOMContents(); -
A few Function tricks function add(x, y) { return x
+ y; } function mul(x, y) { return x * y; } var add4 = add.curry(4); var mul5 = mul.curry(5); add4(3) // 7 mul5(3) // 15 function greet(name) { alert('Hey ' + name + '!'); } greet.delay(1, 'Mark'); // will greet a second later updateDOMContents(); rebindListeners.defer(); // give a tiny moment to breathe, then go! -
Enumerable Object.extend(Array.prototype, Enumerable); Array.prototype._each = function(iterator) { for (var i
= 0; i < this.length; i++) iterator(this[i]); }; Mix it in your type and define _each: -
Enumerable Object.extend(Array.prototype, Enumerable); Array.prototype._each = function(iterator) { for (var i
= 0; i < this.length; i++) iterator(this[i]); }; Mix it in your type and define _each: That’s it! Let’s start with iterations: -
Enumerable Object.extend(Array.prototype, Enumerable); Array.prototype._each = function(iterator) { for (var i
= 0; i < this.length; i++) iterator(this[i]); }; Mix it in your type and define _each: That’s it! Let’s start with iterations: var tae = $w('The Ajax Experience'); -
Enumerable Object.extend(Array.prototype, Enumerable); Array.prototype._each = function(iterator) { for (var i
= 0; i < this.length; i++) iterator(this[i]); }; Mix it in your type and define _each: That’s it! Let’s start with iterations: var tae = $w('The Ajax Experience'); tae.each(alert); -
Enumerable Object.extend(Array.prototype, Enumerable); Array.prototype._each = function(iterator) { for (var i
= 0; i < this.length; i++) iterator(this[i]); }; Mix it in your type and define _each: That’s it! Let’s start with iterations: var tae = $w('The Ajax Experience'); tae.each(alert); tae.each(function(word, index) { alert(index + ': ' + word); }) -
Enumerable Object.extend(Array.prototype, Enumerable); Array.prototype._each = function(iterator) { for (var i
= 0; i < this.length; i++) iterator(this[i]); }; Mix it in your type and define _each: That’s it! Let’s start with iterations: var tae = $w('The Ajax Experience'); tae.each(alert); tae.each(function(word, index) { alert(index + ': ' + word); }) tae.each(function(word, index) { if (index > 1) throw $break; alert(word); }); -
Enumerable (continued) var tae = $w('The Ajax Experience'); tae.map(function(word) {
return word[0]; }) // ['T', 'A', 'E'] -
Enumerable (continued) var tae = $w('The Ajax Experience'); tae.map(function(word) {
return word[0]; }) // ['T', 'A', 'E'] tae.map(function(word) { return word.length; }) // [3, 4, 10] -
Enumerable (continued) var tae = $w('The Ajax Experience'); tae.map(function(word) {
return word[0]; }) // ['T', 'A', 'E'] tae.map(function(word) { return word.length; }) // [3, 4, 10] tae.pluck('length') // [3, 4, 10] -
Enumerable (continued) var tae = $w('The Ajax Experience'); tae.map(function(word) {
return word[0]; }) // ['T', 'A', 'E'] tae.map(function(word) { return word.length; }) // [3, 4, 10] tae.pluck('length') // [3, 4, 10] tae.pluck(0) // ['T', 'A', 'E'] -
Enumerable (continued) var tae = $w('The Ajax Experience'); tae.map(function(word) {
return word[0]; }) // ['T', 'A', 'E'] tae.map(function(word) { return word.length; }) // [3, 4, 10] tae.pluck('length') // [3, 4, 10] tae.pluck(0) // ['T', 'A', 'E'] tae.invoke('gsub', /[aeiouy]+/i, '') // ['Th', 'jx', 'xprnc'] -
Enumerable (continued) var tae = $w('The Ajax Experience'); tae.map(function(word) {
return word[0]; }) // ['T', 'A', 'E'] tae.map(function(word) { return word.length; }) // [3, 4, 10] tae.pluck('length') // [3, 4, 10] tae.pluck(0) // ['T', 'A', 'E'] tae.invoke('gsub', /[aeiouy]+/i, '') // ['Th', 'jx', 'xprnc'] tae.max(function(w) { return w.length; }) // 10 -
Enumerable (continued) var tae = $w('The Ajax Experience'); tae.map(function(word) {
return word[0]; }) // ['T', 'A', 'E'] tae.map(function(word) { return word.length; }) // [3, 4, 10] tae.pluck('length') // [3, 4, 10] tae.pluck(0) // ['T', 'A', 'E'] tae.invoke('gsub', /[aeiouy]+/i, '') // ['Th', 'jx', 'xprnc'] tae.max(function(w) { return w.length; }) // 10 tae.zip(tae.pluck('length')).invoke('reverse') -
Enumerable (continued) var tae = $w('The Ajax Experience'); tae.map(function(word) {
return word[0]; }) // ['T', 'A', 'E'] tae.map(function(word) { return word.length; }) // [3, 4, 10] tae.pluck('length') // [3, 4, 10] tae.pluck(0) // ['T', 'A', 'E'] tae.invoke('gsub', /[aeiouy]+/i, '') // ['Th', 'jx', 'xprnc'] tae.max(function(w) { return w.length; }) // 10 tae.zip(tae.pluck('length')).invoke('reverse') // [[3, 'The'], [4, 'Ajax'], [10, 'Experience']] -
Enumerable (continued) var tae = $w('The Ajax Experience'); tae.map(function(word) {
return word[0]; }) // ['T', 'A', 'E'] tae.map(function(word) { return word.length; }) // [3, 4, 10] tae.pluck('length') // [3, 4, 10] tae.pluck(0) // ['T', 'A', 'E'] tae.invoke('gsub', /[aeiouy]+/i, '') // ['Th', 'jx', 'xprnc'] tae.max(function(w) { return w.length; }) // 10 tae.zip(tae.pluck('length')).invoke('reverse') // [[3, 'The'], [4, 'Ajax'], [10, 'Experience']] tae.inject('', function(acc, s) { return acc + s[0]; }) -
Enumerable (continued) var tae = $w('The Ajax Experience'); tae.map(function(word) {
return word[0]; }) // ['T', 'A', 'E'] tae.map(function(word) { return word.length; }) // [3, 4, 10] tae.pluck('length') // [3, 4, 10] tae.pluck(0) // ['T', 'A', 'E'] tae.invoke('gsub', /[aeiouy]+/i, '') // ['Th', 'jx', 'xprnc'] tae.max(function(w) { return w.length; }) // 10 tae.zip(tae.pluck('length')).invoke('reverse') // [[3, 'The'], [4, 'Ajax'], [10, 'Experience']] tae.inject('', function(acc, s) { return acc + s[0]; }) // 'TAE' -
Text interpolation var tpl = new Template( '#{name} is #{age}
and lives in #{location}'); -
Text interpolation var tpl = new Template( '#{name} is #{age}
and lives in #{location}'); tpl.evaluate({ name: 'Christophe', age: 29, location: 'Paris' }) -
Text interpolation var tpl = new Template( '#{name} is #{age}
and lives in #{location}'); tpl.evaluate({ name: 'Christophe', age: 29, location: 'Paris' }) // 'Christophe is 29 and lives in Paris' -
Text interpolation var tpl = new Template( '#{name} is #{age}
and lives in #{location}'); tpl.evaluate({ name: 'Christophe', age: 29, location: 'Paris' }) // 'Christophe is 29 and lives in Paris' var mislav = { name: 'Mislav', age: 23, location: 'Zagreb' }; -
Text interpolation var tpl = new Template( '#{name} is #{age}
and lives in #{location}'); tpl.evaluate({ name: 'Christophe', age: 29, location: 'Paris' }) // 'Christophe is 29 and lives in Paris' var mislav = { name: 'Mislav', age: 23, location: 'Zagreb' }; tpl.evaluate(mislav) -
Text interpolation var tpl = new Template( '#{name} is #{age}
and lives in #{location}'); tpl.evaluate({ name: 'Christophe', age: 29, location: 'Paris' }) // 'Christophe is 29 and lives in Paris' var mislav = { name: 'Mislav', age: 23, location: 'Zagreb' }; tpl.evaluate(mislav) // 'Mislav is 23 and lives in Zagreb' -
Text interpolation var tpl = new Template( '#{name} is #{age}
and lives in #{location}'); tpl.evaluate({ name: 'Christophe', age: 29, location: 'Paris' }) // 'Christophe is 29 and lives in Paris' var mislav = { name: 'Mislav', age: 23, location: 'Zagreb' }; tpl.evaluate(mislav) // 'Mislav is 23 and lives in Zagreb' // Pending commit to trunk: '#{name} is #{age} and lives in #{location}'.interpolate(mislav) -
Text interpolation var tpl = new Template( '#{name} is #{age}
and lives in #{location}'); tpl.evaluate({ name: 'Christophe', age: 29, location: 'Paris' }) // 'Christophe is 29 and lives in Paris' var mislav = { name: 'Mislav', age: 23, location: 'Zagreb' }; tpl.evaluate(mislav) // 'Mislav is 23 and lives in Zagreb' // Pending commit to trunk: '#{name} is #{age} and lives in #{location}'.interpolate(mislav) // 'Mislav is 23 and lives in Zagreb' -
Ranges new ObjectRange(1, 10).toArray() // [1, 2, 3, 4, 5,
6, 7, 8, 9, 10] -
Ranges new ObjectRange(1, 10).toArray() // [1, 2, 3, 4, 5,
6, 7, 8, 9, 10] $A($R(1, 10)) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] -
Ranges new ObjectRange(1, 10).toArray() // [1, 2, 3, 4, 5,
6, 7, 8, 9, 10] $A($R(1, 10)) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] $R(1, 15).inject(0, function(acc, n) { return acc + n; }) -
Ranges new ObjectRange(1, 10).toArray() // [1, 2, 3, 4, 5,
6, 7, 8, 9, 10] $A($R(1, 10)) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] $R(1, 15).inject(0, function(acc, n) { return acc + n; }) // 120 -
Ranges new ObjectRange(1, 10).toArray() // [1, 2, 3, 4, 5,
6, 7, 8, 9, 10] $A($R(1, 10)) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] $R(1, 15).inject(0, function(acc, n) { return acc + n; }) // 120 $A($R('a', 'm')) -
Ranges new ObjectRange(1, 10).toArray() // [1, 2, 3, 4, 5,
6, 7, 8, 9, 10] $A($R(1, 10)) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] $R(1, 15).inject(0, function(acc, n) { return acc + n; }) // 120 $A($R('a', 'm')) // ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm'] -
Ranges new ObjectRange(1, 10).toArray() // [1, 2, 3, 4, 5,
6, 7, 8, 9, 10] $A($R(1, 10)) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] $R(1, 15).inject(0, function(acc, n) { return acc + n; }) // 120 $A($R('a', 'm')) // ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm'] $R(1, 5, true).map(function(n) { return '*'.times(n); }) -
Ranges new ObjectRange(1, 10).toArray() // [1, 2, 3, 4, 5,
6, 7, 8, 9, 10] $A($R(1, 10)) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] $R(1, 15).inject(0, function(acc, n) { return acc + n; }) // 120 $A($R('a', 'm')) // ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm'] $R(1, 5, true).map(function(n) { return '*'.times(n); }) // ['*', '**', '***', '****'] -
What’s not fun about it? • Is that a trick
question? ‣ Selecting elements for processing -
What’s not fun about it? • Is that a trick
question? ‣ Selecting elements for processing ‣ Traversing it (don’t you hate indent nodes?) -
What’s not fun about it? • Is that a trick
question? ‣ Selecting elements for processing ‣ Traversing it (don’t you hate indent nodes?) ‣ Performing common manipulations (e.g. class tweaking, visibility toggling) -
What’s not fun about it? • Is that a trick
question? ‣ Selecting elements for processing ‣ Traversing it (don’t you hate indent nodes?) ‣ Performing common manipulations (e.g. class tweaking, visibility toggling) ‣ Cross-browser incompatibilities (e.g. attribute reading/writing, style tweaking) -
What’s not fun about it? • Is that a trick
question? ‣ Selecting elements for processing ‣ Traversing it (don’t you hate indent nodes?) ‣ Performing common manipulations (e.g. class tweaking, visibility toggling) ‣ Cross-browser incompatibilities (e.g. attribute reading/writing, style tweaking) ‣ Content changes (update, replace, etc.) -
W3C DOM Level 2 HTML HTMLDocument HTMLElement HTMLHtmlElement HTMLHeadElement HTMLLinkElement
HTMLTitleElement HTMLMetaElement HTMLBaseElement HTMLIsIndexElement HTMLStyleElement HTMLBodyElement HTMLFormElement HTMLSelectElement HTMLOptGroupElement HTMLOptionElement HTMLInputElement HTMLTextAreaElement HTMLButtonElement HTMLLabelElement HTMLFieldSetElement HTMLLegendElement HTMLUListElement HTMLOListElement HTMLDListElement HTMLDirectoryElement HTMLMenuElement HTMLLIElement HTMLDivElement HTMLParagraphElement HTMLHeadingElement HTMLQuoteElement HTMLPreElement HTMLBRElement HTMLBaseFontElement HTMLFontElement HTMLHRElement HTMLModElement HTMLAnchorElement HTMLImageElement HTMLObjectElement HTMLParamElement HTMLAppletElement HTMLMapElement HTMLAreaElement HTMLScriptElement HTMLTableElement HTMLTableCaptionElement HTMLTableColElement HTMLTableSectionElement HTMLTableRowElement HTMLTableCellElement HTMLFrameSetElement HTMLFrameElement HTMLIFrameElement -
W3C DOM Level 2 HTML HTMLDocument HTMLElement HTMLHtmlElement HTMLHeadElement HTMLLinkElement
HTMLTitleElement HTMLMetaElement HTMLBaseElement HTMLIsIndexElement HTMLStyleElement HTMLBodyElement HTMLFormElement HTMLSelectElement HTMLOptGroupElement HTMLOptionElement HTMLInputElement HTMLTextAreaElement HTMLButtonElement HTMLLabelElement HTMLFieldSetElement HTMLLegendElement HTMLUListElement HTMLOListElement HTMLDListElement HTMLDirectoryElement HTMLMenuElement HTMLLIElement HTMLDivElement HTMLParagraphElement HTMLHeadingElement HTMLQuoteElement HTMLPreElement HTMLBRElement HTMLBaseFontElement HTMLFontElement HTMLHRElement HTMLModElement HTMLAnchorElement HTMLImageElement HTMLObjectElement HTMLParamElement HTMLAppletElement HTMLMapElement HTMLAreaElement HTMLScriptElement HTMLTableElement HTMLTableCaptionElement HTMLTableColElement HTMLTableSectionElement HTMLTableRowElement HTMLTableCellElement HTMLFrameSetElement HTMLFrameElement HTMLIFrameElement ✘ Yeah, right. -
Element extension • Element.extend(yourElement) ‣ Adds all of Element.Methods (~60
methods) ‣ Take advantage of prototypes when available -
Element extension • Element.extend(yourElement) ‣ Adds all of Element.Methods (~60
methods) ‣ Take advantage of prototypes when available ‣ Adds tag-specific extensions (e.g. form elements) -
Element extension • Element.extend(yourElement) ‣ Adds all of Element.Methods (~60
methods) ‣ Take advantage of prototypes when available ‣ Adds tag-specific extensions (e.g. form elements) • Most of Prototype extends implicitly ‣ $, $$, all extended methods’ results -
The famous $ function • Most common need: grab an
element by ID -
The famous $ function • Most common need: grab an
element by ID • Runner-ups: -
The famous $ function • Most common need: grab an
element by ID • Runner-ups: ‣ Grab many elements by their ID’s -
The famous $ function • Most common need: grab an
element by ID • Runner-ups: ‣ Grab many elements by their ID’s ‣ Make sure an element is equipped with Prototype’s DOM extensions -
The famous $ function • Most common need: grab an
element by ID • Runner-ups: ‣ Grab many elements by their ID’s ‣ Make sure an element is equipped with Prototype’s DOM extensions • Enter $(...) -
The famous $ function <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p>
<div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
The famous $ function $('article1') <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah
blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
The famous $ function $('article1') // <div id="article1"> <body> <div
id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
The famous $ function $('article1') // <div id="article1"> $(firstDiv) <body>
<div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
The famous $ function $('article1') // <div id="article1"> $(firstDiv) //
firstDiv, with guaranteed extensions <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
The famous $ function $('article1') // <div id="article1"> $(firstDiv) //
firstDiv, with guaranteed extensions $('article1', 'foobar', 'comment101') <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
The famous $ function $('article1') // <div id="article1"> $(firstDiv) //
firstDiv, with guaranteed extensions $('article1', 'foobar', 'comment101') // [firstDiv, null, firstCommentDiv] <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
The famous $ function $('article1') // <div id="article1"> $(firstDiv) //
firstDiv, with guaranteed extensions $('article1', 'foobar', 'comment101') // [firstDiv, null, firstCommentDiv] $('article1', 'foobar', 'comment101').compact() <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
The famous $ function $('article1') // <div id="article1"> $(firstDiv) //
firstDiv, with guaranteed extensions $('article1', 'foobar', 'comment101') // [firstDiv, null, firstCommentDiv] $('article1', 'foobar', 'comment101').compact() // [firstDiv, firstCommentDiv] <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
The famous $ function $('article1') // <div id="article1"> $(firstDiv) //
firstDiv, with guaranteed extensions $('article1', 'foobar', 'comment101') // [firstDiv, null, firstCommentDiv] $('article1', 'foobar', 'comment101').compact() // [firstDiv, firstCommentDiv] $(firstDiv, 'article1', firstCommentDiv).uniq() <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
The famous $ function $('article1') // <div id="article1"> $(firstDiv) //
firstDiv, with guaranteed extensions $('article1', 'foobar', 'comment101') // [firstDiv, null, firstCommentDiv] $('article1', 'foobar', 'comment101').compact() // [firstDiv, firstCommentDiv] $(firstDiv, 'article1', firstCommentDiv).uniq() // [firstDiv, firstCommentDiv] <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
$$ (The Almighty One) • Relies on CSS3 selectors •
Independent of your browser’s CSS3! -
$$ (The Almighty One) • Relies on CSS3 selectors •
Independent of your browser’s CSS3! • Super-fast with DOM3 XPath -
$$ (The Almighty One) • Relies on CSS3 selectors •
Independent of your browser’s CSS3! • Super-fast with DOM3 XPath • Very powerful attributes, classes, ID’s, hierarchy, position, tags/types, and more... -
$$ (The Almighty One) <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p>
<div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
$$ (The Almighty One) $$('div') <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah
blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
$$ (The Almighty One) $$('div') // #article1, .comments, #comment101, //
#article2, .comments, #comment201, // #comment202 <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
$$ (The Almighty One) $$('div') // #article1, .comments, #comment101, //
#article2, .comments, #comment201, // #comment202 $$('div.comments') <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
$$ (The Almighty One) $$('div') // #article1, .comments, #comment101, //
#article2, .comments, #comment201, // #comment202 $$('div.comments') // .comments, .comments <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
$$ (The Almighty One) $$('div') // #article1, .comments, #comment101, //
#article2, .comments, #comment201, // #comment202 $$('div.comments') // .comments, .comments $$('h2 + p') <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
$$ (The Almighty One) $$('div') // #article1, .comments, #comment101, //
#article2, .comments, #comment201, // #comment202 $$('div.comments') // .comments, .comments $$('h2 + p') // <p>Blah blah</p>, <p>Yada yada yada</p> <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
$$ (The Almighty One) $$('div') // #article1, .comments, #comment101, //
#article2, .comments, #comment201, // #comment202 $$('div.comments') // .comments, .comments $$('h2 + p') // <p>Blah blah</p>, <p>Yada yada yada</p> $$('div.comments > div[id$="01"] > h3') <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
$$ (The Almighty One) $$('div') // #article1, .comments, #comment101, //
#article2, .comments, #comment201, // #comment202 $$('div.comments') // .comments, .comments $$('h2 + p') // <p>Blah blah</p>, <p>Yada yada yada</p> $$('div.comments > div[id$="01"] > h3') // h3, h3 <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
$$ (The Almighty One) $$('div') // #article1, .comments, #comment101, //
#article2, .comments, #comment201, // #comment202 $$('div.comments') // .comments, .comments $$('h2 + p') // <p>Blah blah</p>, <p>Yada yada yada</p> $$('div.comments > div[id$="01"] > h3') // h3, h3 $$('div:nth-child(2)') <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
$$ (The Almighty One) $$('div') // #article1, .comments, #comment101, //
#article2, .comments, #comment201, // #comment202 $$('div.comments') // .comments, .comments $$('h2 + p') // <p>Blah blah</p>, <p>Yada yada yada</p> $$('div.comments > div[id$="01"] > h3') // h3, h3 $$('div:nth-child(2)') // #comment202 <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
Traversing and aggregating <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div
class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
Traversing and aggregating $('article1').down() // <h2>Speaking!</h2> <body> <div id="article1"> <h2>Speaking!</h2>
<p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
Traversing and aggregating $('article1').down() // <h2>Speaking!</h2> $('comment101').up() // div.comments <body>
<div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
Traversing and aggregating $('article1').down() // <h2>Speaking!</h2> $('comment101').up() // div.comments $('comment202').previous()
// #comment201 <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
Traversing and aggregating $('article1').down() // <h2>Speaking!</h2> $('comment101').up() // div.comments $('comment202').previous()
// #comment201 $('article1').next().down(1) // div.comments <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
Traversing and aggregating $('article1').down() // <h2>Speaking!</h2> $('comment101').up() // div.comments $('comment202').previous()
// #comment201 $('article1').next().down(1) // div.comments $('article2').down('h3', 1) <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
Traversing and aggregating $('article1').down() // <h2>Speaking!</h2> $('comment101').up() // div.comments $('comment202').previous()
// #comment201 $('article1').next().down(1) // div.comments $('article2').down('h3', 1) // <h3>John @ 09:37am</h3> <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
Traversing and aggregating $('article1').down() // <h2>Speaking!</h2> $('comment101').up() // div.comments $('comment202').previous()
// #comment201 $('article1').next().down(1) // div.comments $('article2').down('h3', 1) // <h3>John @ 09:37am</h3> $('comment201').ancestors() <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
Traversing and aggregating $('article1').down() // <h2>Speaking!</h2> $('comment101').up() // div.comments $('comment202').previous()
// #comment201 $('article1').next().down(1) // div.comments $('article2').down('h3', 1) // <h3>John @ 09:37am</h3> $('comment201').ancestors() // div.comments, #article2, body <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
Traversing and aggregating $('article1').down() // <h2>Speaking!</h2> $('comment101').up() // div.comments $('comment202').previous()
// #comment201 $('article1').next().down(1) // div.comments $('article2').down('h3', 1) // <h3>John @ 09:37am</h3> $('comment201').ancestors() // div.comments, #article2, body $('article2').down().nextSiblings() <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
Traversing and aggregating $('article1').down() // <h2>Speaking!</h2> $('comment101').up() // div.comments $('comment202').previous()
// #comment201 $('article1').next().down(1) // div.comments $('article2').down('h3', 1) // <h3>John @ 09:37am</h3> $('comment201').ancestors() // div.comments, #article2, body $('article2').down().nextSiblings() // p, div.comments <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
Traversing and aggregating $('article1').down() // <h2>Speaking!</h2> $('comment101').up() // div.comments $('comment202').previous()
// #comment201 $('article1').next().down(1) // div.comments $('article2').down('h3', 1) // <h3>John @ 09:37am</h3> $('comment201').ancestors() // div.comments, #article2, body $('article2').down().nextSiblings() // p, div.comments $('article2').down('p').siblings() <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
Traversing and aggregating $('article1').down() // <h2>Speaking!</h2> $('comment101').up() // div.comments $('comment202').previous()
// #comment201 $('article1').next().down(1) // div.comments $('article2').down('h3', 1) // <h3>John @ 09:37am</h3> $('comment201').ancestors() // div.comments, #article2, body $('article2').down().nextSiblings() // p, div.comments $('article2').down('p').siblings() // h2, div.comments <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
Playing with visibility <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div
class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
Playing with visibility var hdr2 = $('article1').down('h2'); <body> <div id="article1">
<h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
Playing with visibility var hdr2 = $('article1').down('h2'); hdr2.visible() // true
<body> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
Playing with visibility var hdr2 = $('article1').down('h2'); hdr2.visible() // true
hdr2.hide() // <h2 style="display: none"> <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
Playing with visibility var hdr2 = $('article1').down('h2'); hdr2.visible() // true
hdr2.hide() // <h2 style="display: none"> hdr2.visible() // false <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
Playing with visibility var hdr2 = $('article1').down('h2'); hdr2.visible() // true
hdr2.hide() // <h2 style="display: none"> hdr2.visible() // false hdr2.toggle() // <h2> <body> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
Playing with attributes <form method="post" action="/signup"> ... <p> <input type="checkbox"
id="chkTOS" name="tos"/> <label for="chkTOS" accesskey="T">I agree, wholeheartedly, to your wonderful terms of service. Love.</label> </p> ... </form> -
Playing with attributes var box = $('chkTOS'); // <input type="checkbox"
id="chkTOS" .../> <form method="post" action="/signup"> ... <p> <input type="checkbox" id="chkTOS" name="tos"/> <label for="chkTOS" accesskey="T">I agree, wholeheartedly, to your wonderful terms of service. Love.</label> </p> ... </form> -
Playing with attributes var box = $('chkTOS'); // <input type="checkbox"
id="chkTOS" .../> box.readAttribute('checked') <form method="post" action="/signup"> ... <p> <input type="checkbox" id="chkTOS" name="tos"/> <label for="chkTOS" accesskey="T">I agree, wholeheartedly, to your wonderful terms of service. Love.</label> </p> ... </form> -
Playing with attributes var box = $('chkTOS'); // <input type="checkbox"
id="chkTOS" .../> box.readAttribute('checked') // null <form method="post" action="/signup"> ... <p> <input type="checkbox" id="chkTOS" name="tos"/> <label for="chkTOS" accesskey="T">I agree, wholeheartedly, to your wonderful terms of service. Love.</label> </p> ... </form> -
Playing with attributes var box = $('chkTOS'); // <input type="checkbox"
id="chkTOS" .../> box.readAttribute('checked') // null box.writeAttribute('checked', true); <form method="post" action="/signup"> ... <p> <input type="checkbox" id="chkTOS" name="tos"/> <label for="chkTOS" accesskey="T">I agree, wholeheartedly, to your wonderful terms of service. Love.</label> </p> ... </form> -
Playing with attributes var box = $('chkTOS'); // <input type="checkbox"
id="chkTOS" .../> box.readAttribute('checked') // null box.writeAttribute('checked', true); // <input type="checkbox" id="chkTOS" checked="checked" .../> <form method="post" action="/signup"> ... <p> <input type="checkbox" id="chkTOS" name="tos"/> <label for="chkTOS" accesskey="T">I agree, wholeheartedly, to your wonderful terms of service. Love.</label> </p> ... </form> -
Playing with attributes var box = $('chkTOS'); // <input type="checkbox"
id="chkTOS" .../> box.readAttribute('checked') // null box.writeAttribute('checked', true); // <input type="checkbox" id="chkTOS" checked="checked" .../> box.readAttribute('checked') <form method="post" action="/signup"> ... <p> <input type="checkbox" id="chkTOS" name="tos"/> <label for="chkTOS" accesskey="T">I agree, wholeheartedly, to your wonderful terms of service. Love.</label> </p> ... </form> -
Playing with attributes var box = $('chkTOS'); // <input type="checkbox"
id="chkTOS" .../> box.readAttribute('checked') // null box.writeAttribute('checked', true); // <input type="checkbox" id="chkTOS" checked="checked" .../> box.readAttribute('checked') // 'checked' <form method="post" action="/signup"> ... <p> <input type="checkbox" id="chkTOS" name="tos"/> <label for="chkTOS" accesskey="T">I agree, wholeheartedly, to your wonderful terms of service. Love.</label> </p> ... </form> -
Playing with attributes var box = $('chkTOS'); // <input type="checkbox"
id="chkTOS" .../> box.readAttribute('checked') // null box.writeAttribute('checked', true); // <input type="checkbox" id="chkTOS" checked="checked" .../> box.readAttribute('checked') // 'checked' box.writeAttribute('checked', false) <form method="post" action="/signup"> ... <p> <input type="checkbox" id="chkTOS" name="tos"/> <label for="chkTOS" accesskey="T">I agree, wholeheartedly, to your wonderful terms of service. Love.</label> </p> ... </form> -
Playing with attributes var box = $('chkTOS'); // <input type="checkbox"
id="chkTOS" .../> box.readAttribute('checked') // null box.writeAttribute('checked', true); // <input type="checkbox" id="chkTOS" checked="checked" .../> box.readAttribute('checked') // 'checked' box.writeAttribute('checked', false) // <input type="checkbox" id="chkTOS" .../> <form method="post" action="/signup"> ... <p> <input type="checkbox" id="chkTOS" name="tos"/> <label for="chkTOS" accesskey="T">I agree, wholeheartedly, to your wonderful terms of service. Love.</label> </p> ... </form> -
Playing with style <style type="text/css"> h2 { font-size: 1.2em; }
</style> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> </div> -
Playing with style var hdr = $('article1').down('h2'); <style type="text/css"> h2
{ font-size: 1.2em; } </style> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> </div> -
Playing with style var hdr = $('article1').down('h2'); hdr.getStyle('font-size'); <style type="text/css">
h2 { font-size: 1.2em; } </style> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> </div> -
Playing with style var hdr = $('article1').down('h2'); hdr.getStyle('font-size'); // '1.2em'
<style type="text/css"> h2 { font-size: 1.2em; } </style> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> </div> -
Playing with style var hdr = $('article1').down('h2'); hdr.getStyle('font-size'); // '1.2em'
hdr.setStyle({ 'font-size': '1.5em' }) <style type="text/css"> h2 { font-size: 1.2em; } </style> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> </div> -
Playing with style var hdr = $('article1').down('h2'); hdr.getStyle('font-size'); // '1.2em'
hdr.setStyle({ 'font-size': '1.5em' }) // <h2 style="font-size: 1.5em"> <style type="text/css"> h2 { font-size: 1.2em; } </style> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> </div> -
Playing with style var hdr = $('article1').down('h2'); hdr.getStyle('font-size'); // '1.2em'
hdr.setStyle({ 'font-size': '1.5em' }) // <h2 style="font-size: 1.5em"> hdr.setStyle({ fontSize: '1.5em', true }) <style type="text/css"> h2 { font-size: 1.2em; } </style> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> </div> -
Playing with style var hdr = $('article1').down('h2'); hdr.getStyle('font-size'); // '1.2em'
hdr.setStyle({ 'font-size': '1.5em' }) // <h2 style="font-size: 1.5em"> hdr.setStyle({ fontSize: '1.5em', true }) // Same, but slightly faster... <style type="text/css"> h2 { font-size: 1.2em; } </style> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> </div> -
Playing with style var hdr = $('article1').down('h2'); hdr.getStyle('font-size'); // '1.2em'
hdr.setStyle({ 'font-size': '1.5em' }) // <h2 style="font-size: 1.5em"> hdr.setStyle({ fontSize: '1.5em', true }) // Same, but slightly faster... hdr.setStyle({ fontSize: null, color: 'red' }) <style type="text/css"> h2 { font-size: 1.2em; } </style> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> </div> -
Playing with style var hdr = $('article1').down('h2'); hdr.getStyle('font-size'); // '1.2em'
hdr.setStyle({ 'font-size': '1.5em' }) // <h2 style="font-size: 1.5em"> hdr.setStyle({ fontSize: '1.5em', true }) // Same, but slightly faster... hdr.setStyle({ fontSize: null, color: 'red' }) // <h2 style="color: red"> <style type="text/css"> h2 { font-size: 1.2em; } </style> <div id="article1"> <h2>Speaking!</h2> <p>Blah blah</p> </div> -
Dynamic contents! var hdr = $('article1').down('h2'); hdr.update('<span>TAE!</span>'); <body> <div id="article1">
<h2>Speaking!</h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
Dynamic contents! var hdr = $('article1').down('h2'); hdr.update('<span>TAE!</span>'); <body> <div id="article1">
<h2><span>TAE!</span></h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
Dynamic contents! var c1 = $('comment101'); c1.insert('<p>Yuck indeed!</p>'); <body> <div
id="article1"> <h2><span>TAE!</span></h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <h3>John @ 12:35pm</h3> <p>yikes!</p> <p>Yuck indeed!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> ... -
Dynamic contents! var c1 = $('comment101'); c1.insert('<p>Yuck indeed!</p>'); c1.insert({ top:
'<p>er, well...</p>' }); <body> <div id="article1"> <h2><span>TAE!</span></h2> <p>Blah blah</p> <div class="comments"> <div id="comment101"> <p>er, well...</p> <h3>John @ 12:35pm</h3> <p>yikes!</p> <p>Yuck indeed!</p> </div> </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> ... -
Dynamic contents! var c1 = $('comment101'); c1.insert('<p>Yuck indeed!</p>'); c1.insert({ top:
'<p>er, well...</p>' }); c1.insert({ before: 'BEFORE', after: 'AFTER' }); <body> <div id="article1"> <h2><span>TAE!</span></h2> <p>Blah blah</p> <div class="comments"> BEFORE <div id="comment101"> <p>er, well...</p> <h3>John @ 12:35pm</h3> <p>yikes!</p> <p>Yuck indeed!</p> </div> AFTER </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> ... -
Dynamic contents! var c1 = $('comment101'); c1.insert('<p>Yuck indeed!</p>'); c1.insert({ top:
'<p>er, well...</p>' }); c1.insert({ before: 'BEFORE', after: 'AFTER' }); c1.replace('<p>--DELETED--</p>'); <body> <div id="article1"> <h2><span>TAE!</span></h2> <p>Blah blah</p> <div class="comments"> BEFORE <p>--DELETED--</p> AFTER </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> </div> </div> </body> -
Explicit element creation var warning = new Element('img', { id:
'warning', src: '/i/warn.png', title: 'Watch out!' }); $(document.body).insert({ top: warning }); <body> <img src="/i/warn.png" id="warning" title="Watch out!"/> <div id="article1"> <h2><span>TAE!</span></h2> <p>Blah blah</p> <div class="comments"> BEFORE <p>--DELETED--</p> AFTER </div> </div> <div id="article2"> <h2>Guess what?</h2> <p>Yada yada yada</p> <div class="comments"> <div id="comment201"> <h3>Mark @ 09:20am</h3> <p>cool!</p> </div> <div id="comment202"> <h3>John @ 09:37am</h3> <p>d'oh!</p> </div> ... -
The problems we face • People keep using events the
DOM0 way ‣ Inline handlers skip the event object in IE -
The problems we face • People keep using events the
DOM0 way ‣ Inline handlers skip the event object in IE • IE vs. The Rest Of The World -
The problems we face • People keep using events the
DOM0 way ‣ Inline handlers skip the event object in IE • IE vs. The Rest Of The World ‣ (De)registering event handlers -
The problems we face • People keep using events the
DOM0 way ‣ Inline handlers skip the event object in IE • IE vs. The Rest Of The World ‣ (De)registering event handlers ‣ Getting / manipulating the event object -
The problems we face • People keep using events the
DOM0 way ‣ Inline handlers skip the event object in IE • IE vs. The Rest Of The World ‣ (De)registering event handlers ‣ Getting / manipulating the event object ‣ Bubbling & capturing, key events, key codes... -
The problems we face • People keep using events the
DOM0 way ‣ Inline handlers skip the event object in IE • IE vs. The Rest Of The World ‣ (De)registering event handlers ‣ Getting / manipulating the event object ‣ Bubbling & capturing, key events, key codes... • Not just IE: Safari/Opera b0rk sometimes -
Unifying the event code • (De)registering handlers ‣ Event.observe, Event.stopObserving
• Manipulating the event object ‣ Event.pointer(X|Y), Event.element, Event.stop, Event.KEY_xxx, etc. -
Unifying the event code • (De)registering handlers ‣ Event.observe, Event.stopObserving
• Manipulating the event object ‣ Event.pointer(X|Y), Event.element, Event.stop, Event.KEY_xxx, etc. • Working around the tweaks ‣ e.g. keypress/keydown in Safari, submit... -
What it looks like function bindPopupLinks() { $$('a.popup').invoke('observe', 'click', handlePopupLinkClick);
} function handlePopupLinkClick(e) { Event.stop(e); var lnk = Event.findElement(e, 'a'); var opts = 'width=500,height=400...'; window.open(lnk.href, 'wndPopupLink', opts); } // Prototype 1.6 should emulate DOMContentLoaded properly... Event.observe(window, 'DOMContentLoaded', bindPopupLinks); -
What it looks like function bindPopupLinks() { $$('a.popup').invoke('observe', 'click', handlePopupLinkClick);
} function handlePopupLinkClick(e) { Event.stop(e); var lnk = Event.findElement(e, 'a'); var opts = 'width=500,height=400...'; window.open(lnk.href, 'wndPopupLink', opts); } // Prototype 1.6 should emulate DOMContentLoaded properly... Event.observe(window, 'DOMContentLoaded', bindPopupLinks); -
What it looks like function bindPopupLinks() { $$('a.popup').invoke('observe', 'click', handlePopupLinkClick);
} function handlePopupLinkClick(e) { Event.stop(e); var lnk = Event.findElement(e, 'a'); var opts = 'width=500,height=400...'; window.open(lnk.href, 'wndPopupLink', opts); } // Prototype 1.6 should emulate DOMContentLoaded properly... Event.observe(window, 'DOMContentLoaded', bindPopupLinks); -
What it looks like function bindPopupLinks() { $$('a.popup').invoke('observe', 'click', handlePopupLinkClick);
} function handlePopupLinkClick(e) { Event.stop(e); var lnk = Event.findElement(e, 'a'); var opts = 'width=500,height=400...'; window.open(lnk.href, 'wndPopupLink', opts); } // Prototype 1.6 should emulate DOMContentLoaded properly... Event.observe(window, 'DOMContentLoaded', bindPopupLinks); -
What it looks like function bindPopupLinks() { $$('a.popup').invoke('observe', 'click', handlePopupLinkClick);
} function handlePopupLinkClick(e) { Event.stop(e); var lnk = Event.findElement(e, 'a'); var opts = 'width=500,height=400...'; window.open(lnk.href, 'wndPopupLink', opts); } // Prototype 1.6 should emulate DOMContentLoaded properly... Event.observe(window, 'DOMContentLoaded', bindPopupLinks); -
X = XML? • Yeah, but let’s just drop the
XML garbage -
X = XML? • Yeah, but let’s just drop the
XML garbage ‣ What’s your cross to bear, SOAP with JS? -
X = XML? • Yeah, but let’s just drop the
XML garbage ‣ What’s your cross to bear, SOAP with JS? • X = XHTML -
X = XML? • Yeah, but let’s just drop the
XML garbage ‣ What’s your cross to bear, SOAP with JS? • X = XHTML ‣ I mean, we all send back XHTML anyway -
X = XML? • Yeah, but let’s just drop the
XML garbage ‣ What’s your cross to bear, SOAP with JS? • X = XHTML ‣ I mean, we all send back XHTML anyway ‣ Unless it’s JSON or plain JS to be eval’d -
X = XML? • Yeah, but let’s just drop the
XML garbage ‣ What’s your cross to bear, SOAP with JS? • X = XHTML ‣ I mean, we all send back XHTML anyway ‣ Unless it’s JSON or plain JS to be eval’d ‣ We’re all doing AHAH, knowing it or not! -
Making an AJAX request new Ajax.Request('/articles/1', { method: 'get', onComplete:
function(xhr) { /* process xhr.responseText */ } }); -
Making an AJAX request new Ajax.Request('/articles/1', { method: 'get', onComplete:
function(xhr) { /* process xhr.responseText */ } }); • Plenty of options! -
Making an AJAX request new Ajax.Request('/articles/1', { method: 'get', onComplete:
function(xhr) { /* process xhr.responseText */ } }); • Plenty of options! ‣ Method, callbacks, parameters, headers, encoding... -
Making an AJAX request new Ajax.Request('/articles/1', { method: 'get', onComplete:
function(xhr) { /* process xhr.responseText */ } }); • Plenty of options! ‣ Method, callbacks, parameters, headers, encoding... new Ajax.Request('/signin', { parameters: { login: 'john', password: 'foobar' }, requestHeaders: { 'X-Auth-Mode': 'XHR' }, onFailure: function(xhr) { alert(xhr.responseText); }, onComplete: function(xhr) { $('loginForm').update(xhr.responseText); } }); -
Returning pure JavaScript? Leave this to me. new Ajax.Request('/persist', {
parameters: $('form').serialize() }); -
Returning pure JavaScript? Leave this to me. new Ajax.Request('/persist', {
parameters: $('form').serialize() }); $('indicator').hide(); $('items').insert('<li id=”item5”>...'); $('item5').highlight.defer(); ... -
Returning pure JavaScript? Leave this to me. new Ajax.Request('/persist', {
parameters: $('form').serialize() }); $('indicator').hide(); $('items').insert('<li id=”item5”>...'); $('item5').highlight.defer(); ... eval(...) -
Submitting forms • At the core of AJAXifying a page...
‣ Using the form’s method and action attributes -
Submitting forms • At the core of AJAXifying a page...
‣ Using the form’s method and action attributes ‣ Serializing it according to HTML spec -
Submitting forms • At the core of AJAXifying a page...
‣ Using the form’s method and action attributes ‣ Serializing it according to HTML spec ‣ Returning JS? You’re done! Otherwise use onComplete. -
Submitting forms • At the core of AJAXifying a page...
‣ Using the form’s method and action attributes ‣ Serializing it according to HTML spec ‣ Returning JS? You’re done! Otherwise use onComplete. • In phase with accessibility / unobstrusive JS -
Submitting forms • The HTML: <form id="signinForm" method="post" action="/signin"> <p><input
type="text" name="login" value="john"/></p> <p><input type="password" name="password" value="foobar"/></p> <p><input type="submit" value="Sign in!"/></p> </form> -
Submitting forms • The HTML: <form id="signinForm" method="post" action="/signin"> <p><input
type="text" name="login" value="john"/></p> <p><input type="password" name="password" value="foobar"/></p> <p><input type="submit" value="Sign in!"/></p> </form> • The JS: -
Submitting forms • The HTML: <form id="signinForm" method="post" action="/signin"> <p><input
type="text" name="login" value="john"/></p> <p><input type="password" name="password" value="foobar"/></p> <p><input type="submit" value="Sign in!"/></p> </form> • The JS: var form = $('signinForm'); form.observe('submit', function(e) { Event.stop(e); form.request(); }); -
AJAX returning markup • It’s everywhere! ‣ Using the form’s
method and action attributes -
AJAX returning markup • It’s everywhere! ‣ Using the form’s
method and action attributes • In essence: container.update(xhr.responseText) -
AJAX returning markup • It’s everywhere! ‣ Using the form’s
method and action attributes • In essence: container.update(xhr.responseText) • Automagically done! -
AJAX returning markup • It’s everywhere! ‣ Using the form’s
method and action attributes • In essence: container.update(xhr.responseText) • Automagically done! ‣ Quite a few cool options (success/failure branching, inserting instead of updating, <script> evaluation) -
AJAX returning markup • It’s everywhere! ‣ Using the form’s
method and action attributes • In essence: container.update(xhr.responseText) • Automagically done! ‣ Quite a few cool options (success/failure branching, inserting instead of updating, <script> evaluation) ‣ Can even be periodical (and slow down if no changes) -
AJAX returning markup new Ajax.Updater('container', '/url'); new Ajax.Updater('todo1', '/todos/1/items', {
insertion: 'bottom', parameters: $('itemForm').serialize() }); -
AJAX returning markup new Ajax.Updater('container', '/url'); new Ajax.Updater('todo1', '/todos/1/items', {
insertion: 'bottom', parameters: $('itemForm').serialize() }); new Ajax.Updater('todo1', '/todos/1', { method: 'get', evalScripts: true, parameters: $('todoForm').serialize() }); -
AJAX returning markup new Ajax.Updater('container', '/url'); new Ajax.Updater('todo1', '/todos/1/items', {
insertion: 'bottom', parameters: $('itemForm').serialize() }); new Ajax.Updater('todo1', '/todos/1', { method: 'get', evalScripts: true, parameters: $('todoForm').serialize() }); new Ajax.Updater({ success: 'todo1', failure: 'errors' }, '/todos/1', { parameters: $('todoForm').serialize() }); -
AJAX returning markup new Ajax.Updater('container', '/url'); new Ajax.Updater('todo1', '/todos/1/items', {
insertion: 'bottom', parameters: $('itemForm').serialize() }); new Ajax.Updater('todo1', '/todos/1', { method: 'get', evalScripts: true, parameters: $('todoForm').serialize() }); new Ajax.Updater({ success: 'todo1', failure: 'errors' }, '/todos/1', { parameters: $('todoForm').serialize() }); new Ajax.Updater({ success: 'todo1' }, '/todos/1', { parameters: $('todoForm').serialize() }); -
Progressive enhancement: “Hijax” • I’ll remind you that your markup
should be ‣ semantical -
Progressive enhancement: “Hijax” • I’ll remind you that your markup
should be ‣ semantical ‣ self-sufficient (works w/o JS) -
Progressive enhancement: “Hijax” • I’ll remind you that your markup
should be ‣ semantical ‣ self-sufficient (works w/o JS) • The recipe is simple: -
Progressive enhancement: “Hijax” • I’ll remind you that your markup
should be ‣ semantical ‣ self-sufficient (works w/o JS) • The recipe is simple: ‣ Plan for AJAX early (controllers/views, etc.) -
Progressive enhancement: “Hijax” • I’ll remind you that your markup
should be ‣ semantical ‣ self-sufficient (works w/o JS) • The recipe is simple: ‣ Plan for AJAX early (controllers/views, etc.) ‣ Implement it when the page works already -
Progressive enhancement: “Hijax” • I’ll remind you that your markup
should be ‣ semantical ‣ self-sufficient (works w/o JS) • The recipe is simple: ‣ Plan for AJAX early (controllers/views, etc.) ‣ Implement it when the page works already ‣ Use unobstrusive JavaScript -
But wait! There’s more! • Next up: Advanced Prototype ‣
More Function-fu -
But wait! There’s more! • Next up: Advanced Prototype ‣
More Function-fu ‣ Best coding practices (e.g. on Enumerable) -
But wait! There’s more! • Next up: Advanced Prototype ‣
More Function-fu ‣ Best coding practices (e.g. on Enumerable) ‣ JSON love -
But wait! There’s more! • Next up: Advanced Prototype ‣
More Function-fu ‣ Best coding practices (e.g. on Enumerable) ‣ JSON love ‣ Whipping up your own DOM extensions -
But wait! There’s more! • Next up: Advanced Prototype ‣
More Function-fu ‣ Best coding practices (e.g. on Enumerable) ‣ JSON love ‣ Whipping up your own DOM extensions ‣ Creating your own classes -
But wait! There’s more! • Next up: Advanced Prototype ‣
More Function-fu ‣ Best coding practices (e.g. on Enumerable) ‣ JSON love ‣ Whipping up your own DOM extensions ‣ Creating your own classes ‣ Sneak peek at Prototype 1.6, and more! -
Online resources • The excellent documentation site ‣ http://prototypejs.org ‣
API reference, tutorials, downloads, blog... ‣ Soon: 3rd-party libs roster -
Online resources • The excellent documentation site ‣ http://prototypejs.org ‣
API reference, tutorials, downloads, blog... ‣ Soon: 3rd-party libs roster • The official list (does script.aculo.us too) ‣ http://groups.google.com/rubyonrails-spinoffs -
Online resources • The excellent documentation site ‣ http://prototypejs.org ‣
API reference, tutorials, downloads, blog... ‣ Soon: 3rd-party libs roster • The official list (does script.aculo.us too) ‣ http://groups.google.com/rubyonrails-spinoffs • IRC ‣ #prototype on Freenode -
Shameless plug • “The Bungee Book” • Available already (as
beta) from the Pragmatic Bookshelf ‣ http://books.pragprog.com/titles/cppsu/ -
Shameless plug • “The Bungee Book” • Available already (as
beta) from the Pragmatic Bookshelf ‣ http://books.pragprog.com/titles/cppsu/ • 95% content-complete already -
Shameless plug • “The Bungee Book” • Available already (as
beta) from the Pragmatic Bookshelf ‣ http://books.pragprog.com/titles/cppsu/ • 95% content-complete already • Up-to-date on the latest stuff -
Shameless plug • “The Bungee Book” • Available already (as
beta) from the Pragmatic Bookshelf ‣ http://books.pragprog.com/titles/cppsu/ • 95% content-complete already • Up-to-date on the latest stuff • Pre-order the paper book too!