Bummer! This is just a preview. You need to be signed in with a Pro account to view the entire video.
Start a free Basic trial
to watch this video
JQuery's "Kill Hitler" Features
50:38 with Dave MethvinThe old joke goes that if you have a time machine the first thing you do is to go back and kill Hitler. jQuery has been around long enough to have features that we killed, even without the benefit of that time machine. Still, it also has features that are best avoided, or at least used with care. Here's an opinionated look at the jQuery API and what things can cause you trouble.
-
0:00
[MUSIC]
-
0:13
The, the reason I call this jQuery's kill Hitler features is
-
0:16
because there's that joke about, you know, if you had a time machine.
-
0:21
Would you go back in time and kill Hitler?
-
0:26
Would that be the first thing that you did?
-
0:27
And if, if I had a time machine and
-
0:30
I was tasked with the job of tweaking jQuery to work better in 2014 these
-
0:35
are the features that I would change in one way or the other.
-
0:42
Or just totally remove, totally kill to make jQuery a better thing for
-
0:47
web development today.
-
0:52
And you know, I, I don't mind saying that jQuery is not perfect.
-
0:57
No language design is perfect.
-
0:58
JavaScript isn't perfect.
-
1:00
I, I think JavaScript gets a bad rap.
-
1:02
That people say, oh you know, it's a horrible language.
-
1:04
But it's really a pretty decent language.
-
1:06
And it's kind of like the rep that Visual Basic used to get back in the 90s.
-
1:13
People did quite a bit of useful work in developing
-
1:19
in developing things with Visual Basic and they have the same with JavaScript.
-
1:24
but, there are things that we can do better.
-
1:26
And, there, there are features in JavaScript that you shouldn't
-
1:30
you shouldn't depend on.
-
1:32
Like things like automatic conversion from string to integer that's a really kind of
-
1:36
a dangerous thing to wat, that you need to watch out for.
-
1:40
And in the same way, jQuery's API has some things seem like a good idea at the time,
-
1:44
turned out they weren't such a good idea.
-
1:48
And to give you an idea of how far we've come, this is,
-
1:52
this is browser market share in 2006.
-
1:54
So in January of 2006, jQuery was announced and released.
-
2:00
And at the time, IE six was 85% of the browsers out there.
-
2:03
Firefox was somewhere around 10-ish%.
-
2:06
And then, you know, even,
-
2:08
there were even browsers like Netscape out there still that people were using.
-
2:13
So, you have to real, you know, think about it,
-
2:16
well browsers have changed a lot in that time and jQuery has had to change as well.
-
2:25
Really the main strength back in 2006 was the browser,
-
2:29
the ability of jQuery to really simplify the act of
-
2:33
writing a web page that worked across all the browsers that were out there.
-
2:37
And really there were two browsers out there.
-
2:39
IE six and Firefox it's around Firefox one or two at the time.
-
2:46
And even when you look at the DOM api's that Firefox was starting to provide.
-
2:54
JQuery made that work easier with this API.
-
2:57
So, even if you liked the DOM APIs that were there, you had to admit that,
-
3:02
gee, jQuery makes it a lot easier to write that code.
-
3:06
Even if I didn't have to worry about cross-browser compatibility,
-
3:09
the ability in jQuery.
-
3:11
To simply do, you know write one line of code and get it
-
3:16
to do what 20 lines of bare JavaScript code would have taken, is a big plus.
-
3:25
Now the other thing that really marks the difference between 2006 and
-
3:30
2014 is Everything was slow back then.
-
3:34
We just weren't doing things anywhere near as ambitious as we
-
3:38
are doing today with JavaScript.
-
3:41
I, I don't think anybody would have expected that you
-
3:44
were writing a high performance, high frame rate games with java script In 2006.
-
3:50
We were mainly using for simple animations.
-
3:53
For, form validation, you know,
-
3:57
in most cases back then the page worked even if JavaScript was disabled.
-
4:03
I, you know there's just no way that 2014 web
-
4:06
pages work with JavaScript disabled most of the time, but back then.
-
4:11
They did work with JavaScript disabled and,
-
4:14
and,part of that was because we were just weren't doing that much with.
-
4:19
But today is very different.
-
4:21
JavaScript is very fast, you don't really have the problems that you have with,
-
4:27
with slow JavaScript anymore.
-
4:30
It's rarely the bottleneck.
-
4:31
You can do things in JavaScript to make your web page or websites or
-
4:36
applications slow.
-
4:37
But it's usually not because you're running too many lines of JavaScript.
-
4:42
And CSS element selection, which is again one of those things that people used to
-
4:45
debate over endlessly, you know.
-
4:47
Is it better to select by class or by ID?
-
4:50
Those kind of things are rarely the bottleneck today either.
-
4:53
So but still we have problems where webpages, especially when loading,
-
4:57
are, are very slow.
-
4:59
Why is that?
-
5:02
Well the main problem is that our pages are flabby, very flabby.
-
5:07
This is a graph composed of information from the http archive.
-
5:11
That shows really a three year period 2010 to 2013 showing what's happened.
-
5:19
And you'll notice actually the green line here which is scripts has started to
-
5:23
flatten out around 200k.
-
5:26
If you look at the size of jQuery it's actually less than 100k.
-
5:30
And so, what's happened is all of the, the,
-
5:37
data that we're transferring is taking longer and longer for the page to load.
-
5:41
Now I have a little more information about that.
-
5:46
This is the data directly from http archive and
-
5:48
this is the most recent data, so this is looking at this year.
-
5:52
And you can see the total transfer size now is up to 1.8 megabytes on,
-
5:58
on the average page.
-
6:00
The total number of requests are hovering just under 100.
-
6:03
So, you know, if you wonder why it's taking so
-
6:05
long for pages to load, here's your answer.
-
6:08
And again if you look at JavaScript.
-
6:11
Were creeping up on 300k of transfer size in 18 files.
-
6:16
And jQuery is just a tiny little piece of that.
-
6:21
And to give you an idea of what the trend is for
-
6:23
jQuery, here's jQuery's growth since 1.2.
-
6:29
And, if you look in this area, this blue line is actually the uncompressed size.
-
6:34
So you really don't wanna look at that because that includes things
-
6:38
like comments.
-
6:39
And you know, like back in the 1.4 days we said gee,
-
6:42
we really should document this stuff better, and we added a bunch of comments.
-
6:47
And that's why it got better.
-
6:48
But once you minify the code, you can see that the growth is much less steep.
-
6:54
But you can also see you're in jQuery 2.0.
-
6:56
We pulled that support for IE six, seven and
-
6:59
eight and we cleaned up the code in general.
-
7:01
And you could see that we got the size down quite a bit.
-
7:05
It's actually minified about a, minified is about 83k.
-
7:10
And minified in G zipped,
-
7:12
which means it's coming over the wire that way is less than 30k.
-
7:17
So the problem with pages getting larger isn't really a jQuery problem.
-
7:22
In fact, we've been going the opposite direction of the trends that we see in
-
7:25
the http archive.
-
7:31
So, the main reason why
-
7:34
our pages are getting flabby is because they're doing very ambitious things.
-
7:39
We're writing full applications and games in browsers as I said.
-
7:43
We really couldn't have imagined that back in 2006 that
-
7:46
we would be doing those kind of things in a webpage or a web app.
-
7:53
And when you do that, some jQuery features that were built back for those days when
-
7:57
we were just kind of dressing up the page, don't really work that well.
-
8:03
But we have to be careful because even if we think something is a bad idea,
-
8:09
if we change it too much we'll break the web.
-
8:12
And, it's a little different than the there,
-
8:15
there's the term break the web there that the browser makers use.
-
8:19
Their problem is that every time a browser automatically updates, if they
-
8:25
break the way something works, the user is facing this problem where they have no
-
8:31
choice, the browser updated they can't go back to the old version they're stuck.
-
8:38
It's a little different with jQuery,
-
8:40
with jQuery, you always have the ability to stay with the old version.
-
8:45
There's a thing with a lot of developers, where psychologically, they don't
-
8:48
like the idea that they're not using the latest version, but they also don't wanna
-
8:52
take any time to actually update to the latest version to fix problems.
-
8:57
Having to do with compatibility issues.
-
8:58
So you can't have both.
-
9:01
You know, with jQuery the good news is if you specifically include a,
-
9:06
a version of jQuery, your code will not break because jQuery updated.
-
9:12
If you decide you wanna use the latest version.
-
9:15
Then you have the, the possibly that you will break your page.
-
9:20
So it's a choice that you get to make.
-
9:23
If you decide you wanna make that choice and update your page.
-
9:27
We love it.
-
9:28
We like everybody using the most recent version.
-
9:30
But, we also realise that.
-
9:32
There's no reason to, if you've got a working set of code,
-
9:35
there's no reason to upgrade just because there's a new version out.
-
9:39
[BLANK_AUDIO]
-
9:41
Now here's some of the patterns I'm gonna go into in
-
9:44
depth in this presentation about, things that you should look out for
-
9:49
when you're writing jQuery code, things like unnecessary reflows,.
-
9:55
Code in critical event paths, browser sniffing,
-
10:00
changes to the global behavior of jQuery, and script injection by HTML.
-
10:05
So let's, let's take a look at each one of these.
-
10:10
One of the most critical things about performance in a webpage, especially once
-
10:15
everything has actually been loaded, is the issue of reflow, or layout.
-
10:21
This is when you make a change in the page, in the HTML and
-
10:26
CSS of the page, that requires the browser to do some redrawing, some re-layout.
-
10:32
If you're not careful about managing that you can cause what's called jank, and jank
-
10:36
is when for example you might be scrolling your page or moving your mouse across
-
10:40
the page, somehow interacting with it and you'll see that things kind of are jerky.
-
10:45
They don't work the way, they don't work smoothly.
-
10:48
There's, there's jumpiness in the animations or
-
10:51
in the scrolling that is generally just makes the user feel like, hey,
-
10:56
something's wrong here because things aren't moving smoothly.
-
11:01
There's a few things that we've done forever to minimize reflow.
-
11:06
In your jQuery code you want to try to minimize changes to the page and
-
11:10
when you do make the changes try to batch them up.
-
11:14
One of the things that's been recommended for years is the idea of detaching.
-
11:18
If you have a lot of changes to make detach those elements from the DOM.
-
11:23
For example let's say you're ent, inserting 1000 elements into a table.
-
11:27
Insert those elements into the table where the table's not in the page and
-
11:32
then append the table to the page rather than inserting each
-
11:38
row into the page one by one where there's the chance that the browser will
-
11:42
actually want to layout the page before the whole table is inserted.
-
11:48
The other thing is classes are best used only for styling.
-
11:52
I see some people who want to use class names to indicate programatically what's
-
11:58
going on in the code, and
-
12:00
the danger there is when you change the classes on elements.
-
12:03
The browser may believe that it, and it may be right, that it
-
12:08
needs to redraw based on the new classes that have been changed independent.
-
12:12
So if you need to make changes that require that you
-
12:15
essentially saving information for example on a plug in, use jQuery's data.
-
12:21
Method to save that type of programmatic information rather than
-
12:25
use classes to do that or you can use data attributes for example.
-
12:32
Now of all the kinds of reflows and layouts, forced ones are the worst.
-
12:38
And this is a case where you make a change to the pay,
-
12:41
you make a change to the page and immediately ask the browser a question
-
12:46
that requires it to recalculate styles and or redraw a page.
-
12:51
And I've got a really simple example here.
-
12:55
The first line of code here is marking comments in an inactive section as hidden.
-
13:03
Might be something you do.
-
13:05
The next thing that it does is it uses the visible selector.
-
13:10
I'm going to assume that, you know,
-
13:12
the first thing this ad class does hidden probably sets display none.
-
13:17
But regardless when you ask whether elements are visible and
-
13:20
you've changed classes in the document the first thing the browser has to
-
13:24
do is go whoa wait a minute.
-
13:25
Something could have changed here.
-
13:27
So it needs to stop what's going on.
-
13:29
Remember the browser in many cases is just single threaded, so
-
13:33
it stops whatever's going on.
-
13:35
JavaScript isn't running, but it yield's control back to the,
-
13:39
to the browser to calculate which elements of class comment are actually visible.
-
13:44
And then I'm applying some text appending some text.
-
13:48
In a real-life situation, this would probably buttons or
-
13:51
some other more complex HTML.
-
13:53
But it's really here, the key thing is that I've used the visible selector.
-
13:59
The alternate way to do this, which we're,
-
14:02
which avoids the forced reflow, is notice the first line is the same.
-
14:07
I add a class, but second line's different.
-
14:10
Instead of actually asking the browser whether that element is visible to it.
-
14:16
It simply asks the browser are there any elements that are of class comment that,
-
14:21
that are not of class hidden.
-
14:23
And if so, I wanna append something to that.
-
14:26
So that prevents the browser from having to do those calculations right in
-
14:31
the middle of my JavaScript.
-
14:32
And that means that in eventually it's going to re, wanna do that calculation.
-
14:37
But it can do it at a more convenient time and
-
14:39
it can also do it when other changes have been made to the doc.
-
14:43
So in short the hidden
-
14:46
invisible selectors which are specific to jQuery, are kind of Kill HItler features.
-
14:51
As, as cool as it seems to say, gee, I'd like to select all the visible ones or
-
14:56
the hidden ones they really don't turn out being as useful,
-
15:00
because they do cause this type of behavior, where the,
-
15:04
the browser needs to recalculate styles and reflow the page.
-
15:09
It's much better to use classes and in general avoid things like hide,
-
15:14
hide and show and then still just add and remove classes to hide and
-
15:19
show things because of that, because of those very issues.
-
15:26
Now another thing that can trick you up is
-
15:29
high-frequency events like mousemove and scroll.
-
15:35
For something like click, it's nearly not very critical.
-
15:39
If you're using if you're clicking on a button,
-
15:44
whatever is going to go on after the click.
-
15:47
It doesn't really matter that if it happened it probably isn't going to
-
15:50
happen for many seconds later.
-
15:52
So those are not time critical.
-
15:54
However, things like mouse movement scroll.
-
15:56
Mouse move every time the mouse moves even a small distance across
-
15:59
the screen it's calling your JavaScript.
-
16:02
Same with scroll every time this page is scrolled even a little
-
16:06
bit it's calling your code.
-
16:08
So you have to be careful what you do in those types of event handlers.
-
16:12
Especially if you attach those event handlers at the document level.
-
16:16
Sometimes people will attach for
-
16:18
example, a mouse move handler at the document level.
-
16:21
That means every time the mouse mouse moves anywhere inside the browser.
-
16:26
On your window you're going to call JavaScript.
-
16:29
And that can be a real performance killer, if you're not careful.
-
16:33
Let me give you an example of things that can really kill performance.
-
16:39
When you, when you are doing things like scrolling, and
-
16:43
your scrolling is going to cause some reflows,
-
16:46
scrolling can actually happen more frequently than every 16 milliseconds.
-
16:51
The browser attempts to maintain about a 60 frame per second rate, which
-
16:55
equates to about every 16 milliseconds, it wants to redraw the screen.
-
17:02
I always find it pretty amazing when you think about how much stuff is
-
17:04
going on that it can redraw the screen in 16 milliseconds.
-
17:10
But it does.
-
17:11
And changing HTML and CSS in a scroll event usually whatever you do in
-
17:17
that scroll event handler should be able to happen in less than a millisecond, and
-
17:21
that leaves 15 milliseconds for
-
17:23
the browser to do ever, whatever it needs to do.
-
17:26
But if you're not careful, for
-
17:28
example if you do something that requires the page to be redrawn.
-
17:32
And let's say the scroll event is being delivered to you three or
-
17:35
four times in a frame.
-
17:37
Then you're making the browser completely recalculate the page three or four times.
-
17:43
Even though it's only going to draw it once.
-
17:45
Now there's a technique with request to
-
17:48
animation frame that allows you to avoid those unnecessary, redraws.
-
17:53
And it goes something like this.
-
17:56
The core, the core thing to remember here is
-
18:00
you want to move the time consuming work outside of the handler, so
-
18:06
the most common thing you would want that people do is, okay when the window
-
18:11
scrolls and then you'll see a big long multi-page piece of code.
-
18:16
That does a bunch of complex things include, including, for
-
18:20
example maybe it's trying to make a little thing you know, stay in the window so
-
18:26
it repositions a window all the time it's maybe a navigation toolbar.
-
18:32
And I didn't show that because it gets really complicated and
-
18:34
it's not relevant to this specific example but a better way to do that is
-
18:40
every time the window scrolls then just set a flag saying we need to redraw and
-
18:47
then when we do need to redraw,
-
18:50
you call requestAnimationFrame and that one times every
-
18:56
16 milliseconds when the browser asks you hey do you need to do anything to draw?
-
18:59
I'm about ready to go to the screen here.
-
19:02
We do your jo, document changes there.
-
19:04
That saves you the three or four unnecessary, redraws, or
-
19:09
recalculations that you would have made the browser do in the meantime.
-
19:12
Now the result of that is better performance, better battery life,
-
19:16
especially in mobile devices.
-
19:17
You know all, all the pluses that you have from avoiding unnecessary work.
-
19:22
Now, I mentioned here that this needs to be shimmed on older browsers if
-
19:25
you’re trying to make your code work for IEA.
-
19:28
The pressed animation frame is not available, so
-
19:30
you’d usually use something like set time out.
-
19:33
But the, the basic logic here would be the same.
-
19:36
There's a great article in the,
-
19:39
HTML5 rock site that goes through how to do this in more detail [SOUND].
-
19:46
So, let's look at another feature that is one that you should definitely avoid.
-
19:51
Especially since we've now taken this out of jQuery as of, version 1.9.
-
19:56
But people still want to do it.
-
19:58
They, people still believe this is necessary and
-
20:02
no matter how hard we rack them they still wanna do it.
-
20:06
But we've just recently seen some changes to browsers that make it
-
20:11
even more crazy to see what, what browsers have to go through because
-
20:16
developers who use browser sniffing try to do things that they shouldn't.
-
20:22
These are four browsers.
-
20:23
The first three are a desktop browser.
-
20:25
And the last one is a mobile browser.
-
20:28
Notice they all say they're Mozilla/5.0.
-
20:31
And that's for compatibility.
-
20:35
Notice that the first three there say they're running under Windows NT
-
20:40
6.3 on WOW64.
-
20:41
That's the user agent way of saying they're running Windows 8.1 and
-
20:47
it's a 64 bit version of Windows.
-
20:51
The first browser string is actually Chrome.
-
20:55
But you notice how many things it says it is, it's Apple webkit, it's KHTML, but
-
20:59
it's like Gecko and it also is Safari.
-
21:03
The second one is Internet Explorer 11, notice that this browser string
-
21:09
does not have MSIE in it, it doesn't have Internet Explorer anywhere in it.
-
21:14
And the reason is because there is so much bad code out there that
-
21:18
does erroneous things when it says, when Internet Explorer identifies itself.
-
21:24
That they found it was better that the browser spring,
-
21:28
string did not say it was Internet Explorer.
-
21:30
But it still says it's like Gecko.
-
21:33
The third one is actually Gecko's engine, it's Firefox.
-
21:38
And again it says it's Gecko and that it's Firefox.
-
21:43
But the last one I think is the ultimate sadness for for browser sniffling,
-
21:51
and that is the most recent update to the Windows phone browser.
-
21:56
And that's how bad it is.
-
22:00
The Windows Phone browser has to say that it's trident, it has touch.
-
22:04
That it's IEMobile, that it's Android.
-
22:07
But it's an iPhone that's like WebKit, like Gecko, that ru,
-
22:11
that is actually Chrome and Mobile Safari.
-
22:13
And you may be thinking, why would it say that?
-
22:17
What possible reason would have to say that?
-
22:20
Well the reason is, because the IE team started doing some triage of mobile sites.
-
22:26
And what they found was,
-
22:28
that unless they put specific strings like mobile Safari into their browser string.
-
22:35
It would not deliver the mobile experience for Windows Phone users.
-
22:40
They also found that, I believe it was
-
22:45
40% to 50% improvement in the Windows Phone experience, if they just lied.
-
22:49
And, they said that it was an Android mobile Safari device.
-
22:55
So it's unfortunate,
-
22:57
this, just thinking about performance this string is more than 200 characters.
-
23:02
But they need to get all these tokens in there so that the code out
-
23:07
there that's written for the mobile web will work properly on a Windows phone.
-
23:12
And, and, that I, I think is really sad.
-
23:14
So, we don't wanna be a part of that.
-
23:19
So there used to be a feature, dollar.browser,
-
23:24
that was actually deprecated pretty long ago, more than four years ago.
-
23:30
That we said, you know, we are gonna deprecate this.
-
23:34
People should not use browser sniffing.
-
23:37
And, in 2009, 2010, every time we'd talk about removing this,
-
23:41
we would hear wails of protest, that there was just no possible way anybody could
-
23:45
do certain things unless they did browser sniffing.
-
23:48
But, obviously, this is the kind of problem you run into when people do
-
23:52
browser sniffing.
-
23:53
So, which problem is worse?
-
23:56
What we did is in jQuery 1.9, we just removed the feature.
-
23:59
So that's about a year ago.
-
24:01
We said, you know, we don't wanna be a part of this and we,
-
24:06
it's been deprecated for four years, so we don't feel bad about removing it.
-
24:13
We still get people who are using the jQuery comp, the compatibility plugin that
-
24:18
we have, jQuery migrate, so that they'll be at dollar.browser.
-
24:23
And I think that's sad.
-
24:24
It'd be much better for people to move away from that.
-
24:28
In particular browser sniffing just doesn't solve a lot of the modern
-
24:31
problems that you have to deal with as a 2014 developer.
-
24:35
And that includes things like, if you're asking a question like,
-
24:38
is this a phone, a tablet, or a desktop?
-
24:41
You may be asking the wrong question.
-
24:43
What if it's an Xbox?
-
24:44
What if it's a Kindle?
-
24:46
What if you're not used to the screen dimensions,
-
24:49
you essentially responsive design, where you build based on the size of the screen,
-
24:54
may be a better way to make decisions than trying to sniff the browser user agent,
-
25:00
where, when you guess wrong, you get a completely bad experience.
-
25:03
Like, if you're trying to sniff to include only mobile devices, then if you're wrong,
-
25:08
you get a desktop experience on a brand new Firefox phone.
-
25:12
That's not a good thing to have happen.
-
25:15
Asking questions like, does this device have a touch screen by looking for
-
25:18
those touch elements or looking for iPad or something.
-
25:23
That's not a good idea either, because probably what you want to know is,
-
25:28
does this device support touch events the way that iPad does.
-
25:32
Or does it support pointer events for touch the way a Windows phone or
-
25:37
a Windows 8 desktop device does.
-
25:41
So, you should be asking if it supports the features that you need to
-
25:45
use in JavaScript.
-
25:46
Not trying to sniff it via the via the browser user agent string.
-
25:52
And if you try to figure out if it has a certain bug or
-
25:54
feature and that bug still exists in the most recent version,
-
25:58
then your code is gonna be wrong when that finally gets patched.
-
26:01
Or changed in behavior.
-
26:03
[BLANK_AUDIO]
-
26:06
So the best solution is feature sniffing.
-
26:09
Modernizr, is a great way to do that.
-
26:13
Some people complain that Modernizr is too big.
-
26:16
They're custom builds.
-
26:17
If you only need three features of Modernizr,
-
26:20
you can build just the pieces you need and not pull in all the feature to text.
-
26:24
And in, specifically you should be doing that,
-
26:27
you shouldn't be using all of Modernizr.
-
26:29
Just use the pieces you need.
-
26:31
It's very easy to create a custom build there,
-
26:34
just with the feature detects you need.
-
26:36
And quite often, it's easy enough to build your own feature detects.
-
26:40
If you want to know if something supports pointer events,
-
26:45
you can look at navigator.max touch points.
-
26:49
And, that tells you if it supports without even having to
-
26:55
use the modernizer.
-
26:59
Now, another feature that seemed like a good idea at the time is
-
27:04
global $.ajax settings.
-
27:07
If you look at what, what the code what this code does,
-
27:11
it looks pretty straight forward, right?
-
27:14
it's, if you know a little bit about AJAX you'd probably would guess this would get
-
27:19
data.text in the same location of the HTML file that the Ajax call is located in.
-
27:27
It will on success, alert the data.
-
27:31
It's just text.
-
27:33
What possibly could go wrong here?
-
27:35
Does anybody have any ideas about what could go wrong?
-
27:37
Well, what could happen is jQuery has some very sane defaults for
-
27:44
all of that stuff that makes a simple case, like the one we look at here, work.
-
27:49
It says when I get data and I pull it in
-
27:56
if I'm gonna look at the headers that come in off the browser off the server and
-
28:01
if it says that the application type is text.
-
28:04
Then I'm going to treat it as text and
-
28:06
I'm gonna then deliver that back to you on success.
-
28:11
Well unfortunately for you it's possible for
-
28:14
someone to change a lot of things about the way an AJAX post is made by default.
-
28:20
So in this case I have completely messed up everyone.
-
28:25
By setting the type to be a post, and the data type to be JSON.
-
28:30
And I've set a very short 100 millisecond timeout.
-
28:34
So, you would think, well who would possibly do this?
-
28:38
Well it happens, a lot of times people say,
-
28:41
I don't wanna have to type all of this stuff every time I make an AJAX request.
-
28:45
So I'll just put it in AJAX setup.
-
28:47
And then they include some third-party plug-in that
-
28:52
expects the defaults to be what they normally are in jQuery.
-
28:56
And then this is the problem they run into.
-
28:59
These are pretty insidious bugs.
-
29:01
You'll occasionally you'll see people coming across this when they
-
29:06
are asking on stack overflow why is my AJAX all hu, hosed up.
-
29:11
And one of the reasons why it's so hard to determine is,
-
29:17
you may have been the person to make these AJAX setup changes, or
-
29:21
it could be a plugin makes these AJAX setup changes.
-
29:25
So, either way, there are going to be places where there are very
-
29:28
straightforward AJAX requests that look very much like this that have
-
29:34
very weird behavior that unless you knew that AJAX set,
-
29:38
setup had been called, it's not gonna behave the way you expected.
-
29:42
In particular the timeout is scary because if you'd
-
29:45
think about how that might work sometimes this request might succeed.
-
29:51
If you just had the timeout change, sometimes this request would succeed but
-
29:56
if, like for example, if the request was cashed, other times it would fail.
-
30:00
But it would seem really random.
-
30:02
Or it might succeed for you, because you're really close to the server.
-
30:04
But it would fail for people who are using your website.
-
30:11
And the basic problem here, is that global AJAX settings don't work.
-
30:16
There really should be no way that, when you're making a request that these
-
30:21
actions at a distance occur, because it makes it
-
30:26
extremely hard to understand what's happening when you make an AJAX request.
-
30:31
Really, it would, it's much better, even if it requires a little more typing, for
-
30:36
people to say exactly what non-default settings they want when
-
30:40
they make a request.
-
30:42
And so that puts AJAX setup in that Kill Hitler category, I think.
-
30:48
But then people say well, then how do I change AJAX, so
-
30:51
that every time I make a request I don't have to type all that.
-
30:55
Well that's really easy, just write your own little function.
-
30:58
And that little function can extend whatever options that you pass in to, for
-
31:03
example, put in the data type of JSON, and a timeout of ten seconds.
-
31:07
So those settings will always be there in your own requests.
-
31:12
But if somebody else calls $.ajax, they get the regular defaults.
-
31:16
You're only gonna change your defaults and not anyone else's defaults.
-
31:23
Another reason to avoid AJAX setup is sometimes people will try to make,
-
31:28
they'll say, oh, I want a global error handler, any time any error happens in a,
-
31:35
in an AJAX request, I wanna know by having this error handler in AJAX set up.
-
31:42
Doesn't really work that way.
-
31:44
What that does is it says by default if no error handler is
-
31:47
provided then this will be used as the error handler.
-
31:51
But that may not be what you want at all.
-
31:53
You may want to say any error on any AJAX request.
-
31:57
We specifically in the documentation say not do this because it
-
32:02
general won't be what you expect.
-
32:05
Instead, we have a really good mechanism already existing to be able to
-
32:10
attach errors on all AJAX requests.
-
32:12
And, those are events, AJAX events are fired for each request.
-
32:17
The first mechanism here is one way to do it.
-
32:20
If you want to be able to add and remove them, I would suggest you use on and
-
32:25
off, and use the AJAX error event name.
-
32:28
And in this case I've added a names base called autocomplete,
-
32:30
with the idea that if I was writing an autocomplete handler autocomplete plugin,
-
32:36
I would be able to attach an AJAX error that had a name space of autocomplete.
-
32:41
And then if somebody wanted to remove it,
-
32:43
I'm only removing the AJAX error handlers for autocomplete.
-
32:47
Anyones that the user added outside my plugin would stay.
-
32:53
So those, that way you can have as many listeners as you want,
-
32:56
and they're all independent of each other.
-
32:58
And you won't have the problem with some other user clobbering your event listener.
-
33:07
So the next point,
-
33:09
the next one is the next Kill Hitler feature is injecting HTML into documents.
-
33:15
We all know that it's great to be
-
33:19
able to use jQuery to create some HTML and just pop it into the document.
-
33:26
There's a, looks like HTML Rule that we use to
-
33:31
determine whether something is HTML or a selector.
-
33:37
And the reason why this is so
-
33:38
important is and I'll show you some examples in just a second.
-
33:43
Any script tags that are inside a document,
-
33:46
will be run when jQuery injects the HTML into the document.
-
33:51
And this is a feature,
-
33:54
unfortunately, that we added pretty early in the evolution of jQuery.
-
34:00
because, there's a lot of situations where you might accidentally inject something
-
34:05
as script, and there's enough people who see it as a feature,
-
34:11
because now I can inject something that's both HTML and script.
-
34:15
The HTML goes into the document and then the script executes after it.
-
34:18
So it's kind of like a templating and
-
34:20
module system, but we've made it easy, maybe too easy.
-
34:27
Let me give you a really simple example here.
-
34:31
And this something not specifically related to jQuery, it can be done,
-
34:35
you can make just as many mistakes in some ways with without jQuery.
-
34:40
We just make it, we just make it so
-
34:41
that the guns pointed directly at your foot immediately.
-
34:46
Here's a simple example if, if you were to wanna get some information off the URL,
-
34:53
you might do something simple like this.
-
34:56
The URL has a, after the hash my name.
-
34:59
And so I'm going to pull that off window location hash and
-
35:04
then I'm going to append that information that I got onto the document.
-
35:10
Here's what the developer had hoped for.
-
35:14
But imagine if it's really a script.
-
35:18
Well when I append to this,
-
35:20
when I append this to the document, I actually run this script.
-
35:24
And that script can do anything that it wants to do.
-
35:28
I mean it can be a giant JavaScript that can steal the cookies off
-
35:32
the current document.
-
35:33
It can send you, redirect you to some bad place.
-
35:37
There's a whole bunch of bad things that can happen here.
-
35:39
And so a developer that assumes that whatever they get from the URL, or
-
35:45
even from information typed in from the user, if they don't guard against it
-
35:49
being HTML and they just append it to the document, bad things happen.
-
35:57
Here's one thing you can do to avoid those cross eyed scripting problems.
-
36:01
And that is I'm going,
-
36:04
I'm still getting whatever the content is and that could have HTML in it.
-
36:09
Or what looks like HTML.
-
36:12
But instead of just dumping it directly in there as a string.
-
36:17
I'm going to find, I'm going to inject kind of a space holder span here and
-
36:21
then I'm going to use the text method rather than the HTML method.
-
36:26
So anything that looks like HTML will simply be rendered as looking like text,
-
36:32
so it's going to convert GTs to greater than symbols.
-
36:36
So we've solved our cross site scripting problem that way.
-
36:40
So jQuery can help you here as well as hurt you.
-
36:42
You just need to be careful when you're doing something with jQuery.
-
36:46
Remember that anything you get from a source outside your own
-
36:52
JavaScript could have scripting in it, and you want to avoid that being executed.
-
37:01
So we made some changes in jQuery 1.9 to
-
37:04
try to make it less likely that you would hurt yourself this way.
-
37:10
The old rules were this,
-
37:12
if a string was passed as a parameter to the jQuery constructor.
-
37:18
JQuery looked to see if it had tags somewhere within the string.
-
37:24
Otherwise it said, well this must be a selector.
-
37:26
So let's look at some examples to get an idea what that meant.
-
37:30
Okay, if I pass this string, that's HTML.
-
37:34
And if I pass this string,
-
37:35
because it does have tags somewhere within the string, that's HTML.
-
37:39
Now this?
-
37:40
That looks like a selector.
-
37:41
And it is a selector.
-
37:43
But look at this one.
-
37:44
Okay, so if I have a title that has something that looks like HTML in it,
-
37:49
the old rule said it was HTML.
-
37:53
And the same, the same thing for this number 6 here.
-
37:57
if I have something that's start and it looks like it might be a selector, but
-
38:02
it turns out it has some stuff in it that's HTMLish, it was HTML.
-
38:08
So, we tried to make it so you are less likely to hurt yourself that way.
-
38:15
A good example of when this could,
-
38:17
could come to bite you is I have a really simple piece of code here.
-
38:21
And, the way I've written the, the variable names,
-
38:25
I seem to be assuming that a selector is coming into the do good function.
-
38:30
And I'm selecting it.
-
38:33
I'm assuming that I'm gonna select it here, and
-
38:35
then I added a class of good to all the elements that I selected.
-
38:40
But, what if I pass in a paragraph?
-
38:45
Then this do good function performs no useful function,
-
38:49
no use at all You know, this is like, well what good does that do?
-
38:55
I've, I've created some elements, I add a class to them, but
-
39:00
I didn't inject them into a document or anything, so basically that was worthless.
-
39:07
So we did some stuff to try to make that a little less likely.
-
39:11
It turns out that the example I just showed you would still occur, but
-
39:14
we've eliminated the cases that we think are more obvious error cases.
-
39:21
The new rules say, okay this is HTML.
-
39:24
But this which starts with text is not HTML.
-
39:29
This is, this is, it'll be interpreted as a, as a selector.
-
39:32
Now it's not a selector.
-
39:34
So you'll get an error immediately saying, hey, you tried to do something here.
-
39:39
Didn't look like HTML to us.
-
39:42
We thought it's should have been a selector, but it wasn't a valid selector.
-
39:46
So you see that all of these bizarro cases here are not selectors anymore.
-
39:54
Well, this case actually is a selector and it would be a valid selector.
-
39:57
But some of these other ones like.
-
40:00
This which has Redskins in it, is not a valid selector, and
-
40:03
you're gonna get an error message here.
-
40:06
This, number four, actually is a valid selector.
-
40:10
But now at least you know.
-
40:13
When you see cases like number two or number six.
-
40:18
Those are not valid selectors you'll know when you get there.
-
40:24
Now what we've done, if you want to parse something as HTML and it's kind of one of
-
40:28
these unusual cases, you can now do it explicitly with a method called parseHTML.
-
40:35
Note that this does not completely solve the cross site scripting issue, because it
-
40:40
is still possible to run HTML fragments via tricks like an On Air attribute.
-
40:46
If you had an HTML that had an On Air attribute, it will run.
-
40:52
So again, still never just blindly inject user or URL input into your document.
-
40:58
That's a recipe for cross-site scripting.
-
41:02
jQuery.
-
41:03
Even when jQuery's not involved something like the on air attribute will,
-
41:08
will cause a problem.
-
41:09
[BLANK_AUDIO]
-
41:12
So I've started, I used the term kill Hitler on this in particular because
-
41:19
it is tempting to think about going back and changing the way things work.
-
41:27
And, you know,
-
41:28
would jQuery be a better library if we took some of these things out.
-
41:33
And actually there's, the joke kill Hitler comes from the URL I've,
-
41:38
I've shown here about someone who actually invents a time machine and
-
41:44
goes back and kills Hitler and he's supposedly posting to a forum.
-
41:50
Constantly people are posting to a forum saying I went back and
-
41:53
I killed Hitler, and then people are going back and
-
41:55
I'm saying, the people who are on the forum frequently.
-
42:00
Users are going back and saying finally I stopped this guy from killing
-
42:03
Hitler because, as you see, no Hitler means no Third Reich.
-
42:08
No World War II.
-
42:10
No rocketry, no electronics, no computers, and therefore no time travel.
-
42:16
So as tempting as it is to think about what it would be like to change jQuery.
-
42:21
It could be that a lot of jQuery's popularity came from some of
-
42:24
these features that at the time seemed like a good idea.
-
42:28
But in 2014 are not such a good idea.
-
42:32
The key is that as a 2014 developer, it's pretty easy for
-
42:37
you to work to avoid those bad parts of the API.
-
42:41
Now that you know what they are.
-
42:42
So you can go and know don't use the hidden invisible selectors.
-
42:47
Don't inject, blindly inject the HTML into your
-
42:51
page from sources that you're not you're not familiar with.
-
42:55
So, you have the ability to make sure that those features that aren't so
-
43:02
good are ones that you avoid, and still be able to get all the benefits of jQuery.
-
43:09
My voice seems to have given up, so that's good because I'm at the end.
-
43:14
I've got plenty of time for questions, and I'd love,
-
43:17
I know there's some questions that people wanted to expand on some things.
-
43:21
That I had shown in the slide so [COUGH] I welcome your questions.
-
43:26
>> Very fast a little, little while ago while you were going over feature
-
43:29
detection and Modernizer if you had any examples with Modernizer.
-
43:34
>> The the Modernizer site has quite a few good examples.
-
43:38
I also would recommend if you're using modernizer and
-
43:42
don't already have a good template, use HTML5 boiler plate.
-
43:49
Modernizer feature detects are pretty simple.
-
43:51
You go to modernizer.com.
-
43:56
Let's see.
-
43:59
Get this out, oops get this out of my way.
-
44:03
[NOISE] And you have the ability to you can build your own copy of it.
-
44:12
You just check off the things you want.
-
44:15
And then build.
-
44:17
And then the documentation shows you everything about,
-
44:22
you know, how it works for example.
-
44:26
So. [BLANK_AUDIO]
-
44:30
It's really it's pretty straightforward to use and
-
44:35
like I said, don't download the whole thing.
-
44:39
I mean when you're in development mode, just include the whole thing.
-
44:42
But when you come to wanting to do it for production,
-
44:44
if all you need is to see whether the old style flexbox and
-
44:50
index db is supported, you can you can include those.
-
44:54
And again, for ones like index db, those are really simple.
-
44:57
I mean you can go look at the, the source for modernizer and
-
45:02
see how simple those feature detects are.
-
45:05
You don't even need to include modernizer for some of these.
-
45:07
Just, it's a one liner to detect, some of these features.
-
45:12
[BLANK_AUDIO]
-
45:18
>> Questions for
-
45:19
Dave, I didn't see any scrolling back to the chat that we didn't cover.
-
45:23
So if you have any other questions for Dave, please enter them in there.
-
45:28
>> Fire away, guys.
-
45:32
What's next for jQuery, Dave?
-
45:39
>> What's next for jQuery.
-
45:41
We are making some changes.
-
45:43
We're looking at actually introducing a new API for doing AJAX-like things.
-
45:51
If you look at if you look at the way AJAX is, I'll go there.
-
45:56
[BLANK_AUDIO]
-
46:04
If you look at the way AJAX is in jQuery.
-
46:10
It's a really co, it's the most complex API we have.
-
46:13
I mean, it goes on and on and on and on.
-
46:18
And one of the reasons why that is,
-
46:21
is because we try to start doing the simple things for you.
-
46:26
And said oh well, most of the time, we can try to figure out what the data type is.
-
46:32
And we can handle this special case and we can handle this special case.
-
46:36
What happened is we all,
-
46:37
we ended up internalizing all of those things and turning them into options.
-
46:41
And that makes it kind of difficult to approach jQuery AJAX,
-
46:47
as just like, if you're lucky it does just what you ask it to,
-
46:50
just like that example I showed in the presentation.
-
46:53
But when we're trying to do certain things,
-
46:56
all of these special cases that are baked into AJAX get in the way.
-
47:03
I mean it, it's not just XML HTTP requests.
-
47:06
It actually handles JSONP and script and all these other things.
-
47:11
So we decided we wanted to create a very tight tiny AJAX request engine.
-
47:18
That tries to avoid a lot of these pitfalls that AJAX has.
-
47:24
And we're hoping we can make it into the next version that'll be out this Fall.
-
47:29
So that if you just want to do a simple AJAX request,
-
47:31
especially if you're doing a custom build you don't even need to
-
47:36
include the full AJAX you can just include the XML HTTP request interface.
-
47:44
We are gonna try to keep everything very tiny and tight still.
-
47:48
So you saw we made some great strides with cutting down the size of the library, and
-
47:53
we still wanna try to do everything within the budget of the bytes that we have.
-
47:58
And we've also already announced that we're dropping support for IE 6 and
-
48:02
IE 7 in the upcoming version.
-
48:05
There just aren't enough people using it.
-
48:07
If you need it, the old versions of jQuery are out there.
-
48:12
>> Cool. Awesome.
-
48:13
A couple of questions came in while you were talking.
-
48:16
Francois asked what do you think of HTML conditional contents that IE added, and
-
48:21
I think they've recently taken them away haven't they?
-
48:25
>> They have I If you look at how conditional comments are used.
-
48:30
If they’re used judiciously I think they’re not a bad way to include for
-
48:33
example to include a special style sheet that works just on IE7
-
48:40
when they’re used the wrong way like if IE and
-
48:45
you’ll see I believe Tumblr still does that.
-
48:50
You know, you'll see ones where they're saying if IE, which is wrong.
-
48:53
Because IE 10, isn't the same as IE 7.
-
48:58
And for that reason, starting with IE 10, they just start ignoring HTML comments.
-
49:06
So I10 I11 if you put them in.
-
49:09
Anything in, in the comments is ignored.
-
49:12
Which is probably the right way to go.
-
49:14
It never was a standard feature.
-
49:15
[BLANK_AUDIO]
-
49:18
>> a, Asimo asks do you see the use of jQuery reduced by NBC frameworks like
-
49:23
Angular or Ember, that already implement a jQuery-like feature and
-
49:28
native features to manipulate the DOM.
-
49:31
>> Actually I think jQuery will still be the foundation of
-
49:35
a lot if not most of the frameworks that are out there.
-
49:41
The Angular team decided to do their own JqLite.
-
49:45
But there are still like the angular
-
49:50
ecosystem really isn't rich enough yet to have the kind of things like jQuery or
-
49:54
UI widgets, and then you want to end up including jQuery.
-
49:58
Also, they're not fully compatible, they built a subset that was enough for
-
50:03
what they needed to do.
-
50:05
Ember, Backbone.
-
50:08
Most of the rest of them use jQuery.
-
50:10
Either they have a hard dependency on jQuery or they are,
-
50:16
you know they're expecting people to include jQuery because they
-
50:18
don't provide any of those types of features.
-
50:21
So, eventually, maybe if things
-
50:26
like web components become common you'd see a reduce use of jQuery but
-
50:31
right now jQuery is used by more than two thirds of the top 10,000 websites.
-
50:36
So it's pretty much all jQuery
You need to sign up for Treehouse in order to download course files.
Sign up