+ All Categories
Home > Technology > Defensive, Cross-Browser Coding with Prototype

Defensive, Cross-Browser Coding with Prototype

Date post: 31-May-2015
Category:
Upload: andrew-dupont
View: 3,354 times
Download: 0 times
Share this document with a friend
Description:
Tired of writing code that breaks in IE? This session explores strategies that Prototype users can employ to write code that works in the real world.
Popular Tags:
92
Defsi, cro-brows codg . prototype . wh
Transcript
Page 1: Defensive, Cross-Browser Coding with Prototype

Defsi, cro-brows codg

.prototype

.wh

Page 2: Defensive, Cross-Browser Coding with Prototype
Page 3: Defensive, Cross-Browser Coding with Prototype

ialm

aly

Page 4: Defensive, Cross-Browser Coding with Prototype

yr co

MSIE

Page 5: Defensive, Cross-Browser Coding with Prototype

T fi ag JavaScript grief

Page 6: Defensive, Cross-Browser Coding with Prototype

#1:“My co wks Fifox, but

fails eryw else…”(peciay IE)

Page 7: Defensive, Cross-Browser Coding with Prototype

Fifox s Fibug, crse

Page 8: Defensive, Cross-Browser Coding with Prototype

Safi sWeb Inspecr

& Drosa

Page 9: Defensive, Cross-Browser Coding with Prototype

Opa sDragfly

hp://w.opa.com/products/dragfly/

Page 10: Defensive, Cross-Browser Coding with Prototype

Inet Expl s…★ Vual Stud★ Microst Script Ed★ Microst Script Debu

Page 11: Defensive, Cross-Browser Coding with Prototype

Ty a...★ Suck

Page 12: Defensive, Cross-Browser Coding with Prototype

Debug regi IE 6–7:

Page 13: Defensive, Cross-Browser Coding with Prototype

a. Do “fmal” bug if

’s yr g

Page 14: Defensive, Cross-Browser Coding with Prototype

b.Use a JavaScript s get tac prompt

Page 16: Defensive, Cross-Browser Coding with Prototype

c.Use MS Script Debu/Ed get ack trac

Page 17: Defensive, Cross-Browser Coding with Prototype
Page 18: Defensive, Cross-Browser Coding with Prototype

d.Do old-fhn“a” bug

Page 19: Defensive, Cross-Browser Coding with Prototype

#2:“OK, my co wks IE now,

but slow counicg by tegraph.”

Page 20: Defensive, Cross-Browser Coding with Prototype

Plu: Use Fibug prir

Page 21: Defensive, Cross-Browser Coding with Prototype

Pr-m’s prilg: msu elaps me betwn

y two co pots

Page 22: Defensive, Cross-Browser Coding with Prototype

var Timer = Class.create({ initialize: function() { this.startTime = new Date().valueOf(); }, end: function() { var endTime = new Date().valueOf(); return (this.startTime - endTime).toString(); } });

Page 23: Defensive, Cross-Browser Coding with Prototype

function doSomethingCostly() { var timer1 = new Timer(); // do something that takes forever... alert(timer1.end());}

Page 24: Defensive, Cross-Browser Coding with Prototype

Ce udy:zebra-ripg a tab

Page 25: Defensive, Cross-Browser Coding with Prototype

Two tts:1 rows 10 rows

Page 26: Defensive, Cross-Browser Coding with Prototype

var TableStriper = Class.create({ initialize: function(element) { var timer = new Timer(); this.element = $(element); this.element.select("tbody > tr:nth-child(even)") .invoke("addClassName", "row-even"); this.element.select("tbody > tr:nth-child(odd)") .invoke("addClassName", "row-odd");

alert(timer.end() + "ms"); }});

Revn 1

Page 27: Defensive, Cross-Browser Coding with Prototype

1 Rows 10 Rows

Safi

Fifox

IE

14ms 603ms

26ms 367ms

341ms 5268ms

Page 28: Defensive, Cross-Browser Coding with Prototype

:-(

Page 29: Defensive, Cross-Browser Coding with Prototype

var TableStriper = Class.create({ initialize: function(element) { var timer = new Timer(); this.element = $(element); // Why use two selectors? Use one and loop instead var trs = this.element.select("tbody > tr"); trs.each( function(tr, i) { tr.addClassName(i % 2 == 0 ? "row-odd" : "row-even"); });

alert(timer.end() + "ms"); }});

Revn 2

Page 30: Defensive, Cross-Browser Coding with Prototype

1 Rows 10 Rows

Safi

Fifox

IE

12ms 123ms

14ms 130ms

160ms 3245ms

Page 31: Defensive, Cross-Browser Coding with Prototype

:-(

Page 32: Defensive, Cross-Browser Coding with Prototype

var TableStriper = Class.create({ initialize: function(element) { var timer = new Timer(); this.element = $(element); // Eschew CSS selector syntax altogether var tbodies = this.element.getElementsByTagName("tbody"); for (var i = 0, tbody, trs; i < tbodies.length; i++) { tbody = tbodies[i]; trs = tbody.getElementsByTagName("tr"); for (var j = 0, tr; j < trs.length; j++) { tr = trs[j]; // filter out anything that isn't a direct child of the TBODY if (tr.parentNode !== tbody) continue; $(tr).addClassName(j % 2 == 0 ? "row-odd" : "row-even"); } } alert(timer.end() + "ms"); }});

Revn 3

Page 33: Defensive, Cross-Browser Coding with Prototype

1 Rows 10 Rows

Safi

Fifox

IE

5ms 53ms

13ms 143ms

160ms 31ms

Page 34: Defensive, Cross-Browser Coding with Prototype

:-(

Page 35: Defensive, Cross-Browser Coding with Prototype

Revn 4var TableStriper = Class.create({ initialize: function(element) { var timer = new Timer(); this.element = $(element); var tbodies = this.element.getElementsByTagName("tbody"); for (var i = 0, tbody, trs; i < tbodies.length; i++) { tbody = tbodies[i]; trs = tbody.rows; // there's a pre-defined collection for this for (var j = 0, tr; j < trs.length; j++) { tr = trs[j]; // OK, we'll stop "extending" elements now tr.className += j % 2 == 0 ? " row-odd" : " row-even"; } } alert(timer.end() + "ms"); }});

Page 36: Defensive, Cross-Browser Coding with Prototype

1 Rows 10 Rows

Safi

Fifox

IE

1ms 8ms

6ms 64ms

20ms 141ms

Page 37: Defensive, Cross-Browser Coding with Prototype

HOLY CRAP

Page 38: Defensive, Cross-Browser Coding with Prototype

So wt did we n?

Page 39: Defensive, Cross-Browser Coding with Prototype

Ls 1:Secr/$$ e opmiz, but

wh gh nos page, y’ brg s kns

Page 40: Defensive, Cross-Browser Coding with Prototype

Ls 2:“Extdg” emts slow,

do not sca lge no sult sets

Page 41: Defensive, Cross-Browser Coding with Prototype

$ Emt.extd e shrk-wrap f syacc sug

Page 42: Defensive, Cross-Browser Coding with Prototype

In morn browss, Emt.extd do nog; IE 6–7 s copy ce

meods muay

Page 43: Defensive, Cross-Browser Coding with Prototype

A Protype DOM lps use

Emt.extd tnay

Page 44: Defensive, Cross-Browser Coding with Prototype

Wa — sayg“D’t use Protype?”

Page 45: Defensive, Cross-Browser Coding with Prototype

We, y:Rch tsi

yr abrac wn ’s o coly

Page 46: Defensive, Cross-Browser Coding with Prototype

Ls #3:A lt b opmizn

go a lg way

Page 47: Defensive, Cross-Browser Coding with Prototype

A few l n-Protype co, wrt

ce, c mn hugepfmce gas

Page 48: Defensive, Cross-Browser Coding with Prototype

As always,opmizn si

lps pays f big

Page 49: Defensive, Cross-Browser Coding with Prototype

Ls 4:Once “fls” ft… op!

Page 50: Defensive, Cross-Browser Coding with Prototype

#3:“Now ’s py ft IE, but

if I a my page op wn I go lunch, IE’s froz by me

I get back.”

Page 51: Defensive, Cross-Browser Coding with Prototype

Memy aks

Page 52: Defensive, Cross-Browser Coding with Prototype

Not ju IE 6.0;IE 7.0 claims fix m, but ’s a li

Page 53: Defensive, Cross-Browser Coding with Prototype

Y c’t be su yr co ak-fe uil y try

al wld

Page 54: Defensive, Cross-Browser Coding with Prototype

Framewks c lpa lt, but ’s i

yr probm sol

Page 56: Defensive, Cross-Browser Coding with Prototype

Load yr page; wch memy usage ce

(’s nmal)

Page 57: Defensive, Cross-Browser Coding with Prototype

Now load page; if memy usage c

aga, ’s bad

Page 58: Defensive, Cross-Browser Coding with Prototype

A y seg expo no prop

fce n-primis?( “circul fce” ak)

Page 59: Defensive, Cross-Browser Coding with Prototype

A y signg es tsi Protype’s

e APIs?(e.g., emt.click)

Page 60: Defensive, Cross-Browser Coding with Prototype

(Protype’s e syem wi c up aft self, but if y

d’t use , y’ yr own)

Page 61: Defensive, Cross-Browser Coding with Prototype

A y cag Emt.place Emt.upde ctt

wh tacd lts?

Page 62: Defensive, Cross-Browser Coding with Prototype

If so, tach lts fir

Page 63: Defensive, Cross-Browser Coding with Prototype

foo.stopObserving("click", respondToFoo);// (detaches the respondToFoo listener)

foo.stopObserving("click");// (detaches all click listeners on foo)

foo.stopObserving();// (detaches _all_ listeners on foo)

Page 64: Defensive, Cross-Browser Coding with Prototype

Protype 1.6.[next]wi lp y t wh

Page 65: Defensive, Cross-Browser Coding with Prototype

FAL TT:L IE rug

ornight

Page 66: Defensive, Cross-Browser Coding with Prototype

#4:“My a pfms wrfuy

now, but my co lks likehum suffg.”

Page 67: Defensive, Cross-Browser Coding with Prototype

In or wds:“How do I kp my co mataab if s

a f a esespecial c?”

Page 68: Defensive, Cross-Browser Coding with Prototype

Wt if we cld kpa ugly cks

e place?

Page 69: Defensive, Cross-Browser Coding with Prototype

Func#wrap

Page 70: Defensive, Cross-Browser Coding with Prototype

Origal func

function getOffsetTop(element) { return element.offsetTop;}

Page 71: Defensive, Cross-Browser Coding with Prototype

Modifi func

getOffsetTop = getOffsetTop.wrap( function(proceed, element) { // IE will report incorrect values // unless we give the element "layout" if (!element.currentStyle.hasLayout == -1) element.style.zoom = 1;

return proceed(element); });

Page 72: Defensive, Cross-Browser Coding with Prototype

Only IE nds s modifi rsn

Page 73: Defensive, Cross-Browser Coding with Prototype

Y c kp “wra” funcs

e place

Page 74: Defensive, Cross-Browser Coding with Prototype

<script src="js/prototype.js" type="text/javascript"></script> <script src="js/main.js" type="text/javascript"></script> <!--[if IE]> <script src="js/ie-only.js" type="text/javascript"></script> <![endif]-->

Page 75: Defensive, Cross-Browser Coding with Prototype

BUT: L a lpful cot

Page 76: Defensive, Cross-Browser Coding with Prototype

// NOTE: Redefined in ie-only.jsfunction getOffsetTop(element) { return element.offsetTop;}

Page 77: Defensive, Cross-Browser Coding with Prototype

ALSO:Expla why y nd

wk

Page 78: Defensive, Cross-Browser Coding with Prototype

getOffsetTop = getOffsetTop.wrap( function(proceed, element) { // IE will report incorrect values // unless we give the element "layout" if (!element.currentStyle.hasLayout == -1) element.style.zoom = 1;

return proceed(element); });

Page 79: Defensive, Cross-Browser Coding with Prototype

In or wds,co d...

Page 80: Defensive, Cross-Browser Coding with Prototype

…n wre co adapts/dgs do quid msagg

Page 81: Defensive, Cross-Browser Coding with Prototype

#5:“G! My co lks gd I fl. How do I avoid er

vg do aga?”

Page 82: Defensive, Cross-Browser Coding with Prototype

EAT YR GETABS(wre un tts &funcal tts)

Page 83: Defensive, Cross-Browser Coding with Prototype

“Oh, I’d lo , but I wk UI lay, y c’t ay un-tt

tface… , yway, we e way o rapidly do tt-drin

lopmt… but y’ right, I’ tay wre a ful tts sn I get

some me…”

Page 84: Defensive, Cross-Browser Coding with Prototype

SPE ME YR EXCUS

Page 85: Defensive, Cross-Browser Coding with Prototype

Wre un tts f eryg c be

aum

Page 86: Defensive, Cross-Browser Coding with Prototype

Protype do ; script.aculo.us do ;

y c do , o

Page 87: Defensive, Cross-Browser Coding with Prototype

Wre funcal tts make si f

hums tt

Page 88: Defensive, Cross-Browser Coding with Prototype

QA wi buy y a py

Page 89: Defensive, Cross-Browser Coding with Prototype

Wre “diy” tts(row curbas

yr co)

Page 90: Defensive, Cross-Browser Coding with Prototype

Wt abt Senium?WIR?

(cd play hum put)

Page 91: Defensive, Cross-Browser Coding with Prototype

meh.


Recommended