You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: page/code-organization/feature-browser-detection.md
+3-3
Original file line number
Diff line number
Diff line change
@@ -58,7 +58,7 @@ There are several ways to go about feature detection:
58
58
59
59
Let's take a look at how to check whether or not a `<canvas>` element exists in a specific browser, without using a helper library. We do this by specifically querying whether the method or property exists:
60
60
61
-
```js
61
+
```
62
62
// We want to show a graph in browsers that support canvas, but a data table in browsers that don't.
63
63
var elem = document.createElement("canvas");
64
64
@@ -93,7 +93,7 @@ Thankfully, there are some great helper libraries (like [Modernizr](http://moder
93
93
94
94
For example, utilizing Modernizr, we are able to do the same canvas detection test with this code:
95
95
96
-
```js
96
+
```
97
97
if ( Modernizr.canvas ) {
98
98
99
99
showGraphWithCanvas();
@@ -113,7 +113,7 @@ So, while the Modernizr syntax is great, it can end up being quite cumbersome to
113
113
114
114
The Modernizr object exposes a `load()` method that many prefer over the syntax mentioned previously. This is due to the another library that Modernizr now uses internally: [yepnope](http://yepnopejs.com/). Testing for canvas can now become something like this:
The `$('#helloBtn')` code selects the button element using the `$` (aka `jQuery`) function and returns a jQuery object. The jQuery object has a bunch of methods (functions) available to it, one of them named `click`, which resides in the jQuery object's prototype. We call the `click` method on the jQuery object and pass along an anonymous function event handler that's going to be executed when a user clicks the button, alerting "Hello." to the user.
68
+
The `$("#helloBtn")` code selects the button element using the `$` (aka `jQuery`) function and returns a jQuery object. The jQuery object has a bunch of methods (functions) available to it, one of them named `click`, which resides in the jQuery object's prototype. We call the `click` method on the jQuery object and pass along an anonymous function event handler that's going to be executed when a user clicks the button, alerting "Hello." to the user.
69
69
70
70
There are a number of ways that events can be listened for using jQuery:
71
71
@@ -169,7 +169,7 @@ Finally, to inspect the event itself and see all of the data it contains, you sh
169
169
170
170
```
171
171
//Logging an event's information
172
-
$('form').on( 'submit', function(event) {
172
+
$("form").on( "submit", function(event) {
173
173
174
174
event.preventDefault(); // Prevent the form's default submission.
Copy file name to clipboardExpand all lines: page/faq/add_keyboard_navigation.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -170,7 +170,7 @@ In Mathieu’s code, he had the following:
170
170
});
171
171
```
172
172
173
-
Note that $().ready is the same as $(document).ready (though it’s not a style I would personally use). *Directly* after the plugin (i.e. before the });) results in the following code:
173
+
Note that $().ready is the same as $(document).ready (though it’s not a style I would personally use). *Directly* after the plugin (i.e. before the });) results in the following code:
Copy file name to clipboardExpand all lines: page/faq/enable_the_back_button.md
+4-4
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,7 @@ Now armed with Ben’s plugin, we’re going to refactor the tab code so that th
22
22
23
23
The way the existing tabs work is as follows:
24
24
25
-
- Collect all the tab panels using $('div.tabs > div') and initialise to show the first tab1
25
+
- Collect all the tab panels using $("div.tabs > div") and initialise to show the first tab1
26
26
- Listen for clicks on the links that form the actual tabs
27
27
- When a tab is clicked, hide all the tab panels, filter down to the one we wanted to see and show it, then update the classes on the tabs so the current link appears to be focused
28
28
- Finally, initialise by finding the first tab and triggering a click
@@ -34,7 +34,7 @@ This process was our original code, and most of it needs to stay in place. The c
34
34
So we listen for the hashchange event, just like we might listen for a click event:
Now we move all of the original code from the click handler in to this new event listener. I’ve copied this code in, and marked in bold which lines we’ll need to change:
Copy file name to clipboardExpand all lines: page/performance/append-outside-loop.md
+21-12
Original file line number
Diff line number
Diff line change
@@ -10,9 +10,12 @@ Touching the DOM comes at a cost; if you're appending a lot of elements to the
10
10
DOM, you will want to append them all at once, rather then one at a time. This is common problem when appending elements within a loop.
11
11
12
12
```
13
-
$.each(myArray, function(i, item) {
14
-
var newListItem = '<li>' + item + '</li>';
15
-
$('#ballers').append(newListItem);
13
+
$.each( myArray, function( i, item ) {
14
+
15
+
var newListItem = "<li>" + item + "</li>";
16
+
17
+
$("#ballers").append( newListItem );
18
+
16
19
});
17
20
```
18
21
@@ -23,26 +26,32 @@ element. After the loop, just append the fragment to the DOM element.
23
26
```
24
27
var frag = document.createDocumentFragment();
25
28
26
-
$.each(myArray, function (i, item) {
29
+
$.each( myArray, function( i, item ) {
30
+
27
31
var newListItem = document.createElement("li");
28
-
var itemText = document.createTextNode(item);
29
-
newListItem.appendChild(itemText);
30
-
frag.appendChild(newListItem);
32
+
var itemText = document.createTextNode( item );
33
+
34
+
newListItem.appendChild( itemText );
35
+
36
+
frag.appendChild( newListItem );
37
+
31
38
});
32
39
33
-
$('#ballers')[0].appendChild(frag);
40
+
$("#ballers")[ 0 ].appendChild(frag);
34
41
```
35
42
36
43
Another technique, which is quite simple, is to build up a string during each iteration of the loop. After the loop, just set the html of the DOM element to that string.
37
44
38
45
```
39
-
var myHtml = '';
46
+
var myHtml = "";
47
+
48
+
$.each( myArray, function( i, item ) {
49
+
50
+
myHtml += "<li>" + item + "</li>";
40
51
41
-
$.each(myArray, function(i, item) {
42
-
myHtml += '<li>' + item + '</li>';
43
52
});
44
53
45
-
$('#ballers').html(myHtml);
54
+
$("#ballers").html(myHtml);
46
55
```
47
56
48
57
There are of course other techniques you could certainly test out; a great way to test the performance of these is through a site called [jsperf](http://jsperf.com). This site allows you to benchmark each technique and visually see how it performs across all the browsers.
0 commit comments