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/about-jquery/how-jquery-works.md
+72-70
Original file line number
Diff line number
Diff line change
@@ -5,8 +5,8 @@ level: beginner
5
5
### jQuery: The Basics
6
6
7
7
This is a basic tutorial, designed to help you get started using jQuery. If you
8
-
don't have a test page setup yet, start by creating a new HTML page with the
9
-
following contents:
8
+
don't have a test page setup yet, start by creating the following HTML page:
9
+
10
10
```
11
11
<!doctype html>
12
12
<html>
@@ -18,33 +18,32 @@ following contents:
18
18
<a href="http://jquery.com/">jQuery</a>
19
19
<script src="jquery.js"></script>
20
20
<script>
21
-
var foo = "bar";
21
+
// Your code goes here
22
22
</script>
23
23
</body>
24
24
</html>
25
25
```
26
26
27
-
Edit the `src` attribute in the script tag to point to your copy of jquery.js.
28
-
For example, if jquery.js is in the same directory as your HTML file, you
29
-
can use:
30
-
```
31
-
<script src="jquery.js"></script>
32
-
```
33
-
34
-
Download a copy of jQuery from the [Downloading jQuery](http://jquery.com/download/) page.
27
+
The `src` attribute in the `<script>` element must point to a copy of jQuery.
28
+
Download a copy of jQuery from the [Downloading jQuery](http://jquery.com/download/) page
29
+
and store the `jquery.js` file in the same directory as your HTML file.
35
30
36
31
### Launching Code on Document Ready
37
-
The first thing that most Javascript programmers end up doing is adding some code to their program, similar to this:
32
+
33
+
To ensure that their code runs after the browser finishes loading the document,
34
+
many JavaScript programmers wrap their code in an `onload` function:
35
+
38
36
```
39
37
window.onload = function() {
40
38
41
39
alert("welcome");
42
40
43
41
}
44
42
```
45
-
Inside of which is the code that you want to run right when the page is loaded. Problematically, however, the Javascript code isn't run until all images are finished downloading (this includes banner ads). The reason for using `window.onload` in the first place is that the HTML 'document' isn't finished loading yet, when you first try to run your code.
46
43
47
-
To circumvent both problems, jQuery has a simple statement that checks the `document` and waits until it's ready to be manipulated, known as the [ ready event ](http://api.jquery.com/ready):
44
+
Unfortunately, the code doesn't run until all images are finished downloading, including banner ads.
45
+
To run code as soon as the `document` is ready to be manipulated, jQuery has a statement
46
+
known as the [ ready event ](http://api.jquery.com/ready):
Save your HTML file and reload the test page in your browser. Clicking the link on the page should make a browser's alert pop-up, before leaving to go to the main jQuery page.
71
69
72
-
For click and most other [events](http://api.jquery.com/category/events/), you can prevent the default behaviour - here, following the link to jquery.com - by calling event.preventDefault() in the event handler:
70
+
Save your HTML file and reload the test page in your browser.
71
+
Clicking the link should now first display an alert pop-up,
72
+
then continue with the default behavior of navigating to http://jquery.com.
73
+
74
+
For `click` and most other [events](http://api.jquery.com/category/events/),
75
+
you can prevent the default behavior by calling `event.preventDefault()` in the event handler:
alert("The link will no longer take you to jquery.com");
114
+
alert("The link will no longer take you to jquery.com");
114
115
115
-
event.preventDefault();
116
+
event.preventDefault();
116
117
117
-
});
118
+
});
118
119
119
-
});
120
+
});
120
121
121
-
</script>
122
-
</body>
122
+
</script>
123
+
</body>
123
124
</html>
124
125
```
125
126
126
127
### Adding and Removing an HTML Class
127
128
128
-
**Important:***The remaining jQuery examples will need to be placed inside the ready event so that they are executed when the document is ready to be worked on.*
129
+
**Important:***You must place the remaining jQuery examples inside the `ready` event so that your code executes when the document is ready to be worked on.*
129
130
130
-
Another common task is adding (or removing) a `class`.
131
+
Another common task is adding or removing a `class`.
131
132
132
-
First, add some style information into the `head` of the document, like this:
133
+
First, add some style information into the `<head>` of the document, like this:
133
134
134
135
```
135
136
<style>
@@ -139,25 +140,25 @@ First, add some style information into the `head` of the document, like this:
139
140
</style>
140
141
```
141
142
142
-
Next, add the [addClass](http://api.jquery.com/addClass) call to the script:
143
+
Next, add the [addClass()](http://api.jquery.com/addClass) call to the script:
143
144
144
145
```
145
146
$("a").addClass("test");
146
147
```
147
148
148
-
All `a` elements will now be bold.
149
+
All `a` elements are now bold.
149
150
150
-
To remove the `class`, use [removeClass](http://api.jquery.com/removeClass):
151
+
To remove an existing `class`, use [removeClass()](http://api.jquery.com/removeClass):
151
152
152
153
```
153
154
$("a").removeClass("test");
154
155
```
155
156
156
157
### Special Effects
157
158
158
-
In jQuery, a couple of handy [effects](http://api.jquery.com/category/effects/)
159
-
are provided, to really make web sites stand out. For example,
160
-
change the click you added earlier to the following:
159
+
jQuery also provides some handy [effects](http://api.jquery.com/category/effects/)
Now, the link should slowly disappear when clicked.
173
+
then the link slowly disappears when clicked.
173
174
174
175
## Callbacks and Functions
175
176
176
-
A callback is a function that is passed as an argument to another function and
177
-
is executed after its parent function has completed. The special thing about a
178
-
callback is that functions that appear after the "parent" can execute before
179
-
the callback executes. Another important thing to know is how to properly pass
180
-
the callback.
177
+
Unlike many other programming languages, JavaScript enables you to freely pass functions around to be executed at a later time.
178
+
A *callback* is a function that is passed as an argument to another function and
179
+
is executed after its parent function has completed. Callbacks are special because
180
+
they patiently wait to execute until their parent finishes.
181
+
Meanwhile, the browser can be executing other functions or doing all sorts of other work.
182
+
183
+
To use callbacks, it is important to know how to pass them into their parent function.
181
184
182
185
### Callback *without* Arguments
183
186
184
-
Pass callbacks with no arguments like this:
187
+
If a callback has no arguments, you can pass it in like this:
185
188
186
189
```
187
190
$.get( "myhtmlpage.html", myCallBack );
188
191
```
189
192
190
-
**Note** that the second parameter here is simply the function name (but *not* as a string and without parentheses). Functions in Javascript are 'First class citizens' and can be passed around like variable references and executed at a later time.
193
+
When [$.get](http://api.jquery.com/jQuery.get/) finishes getting the page `myhtmlpage.html`, it executes the `myCallBack` function.
194
+
**Note** that the second parameter here is simply the function name (but *not* as a string and without parentheses).
191
195
192
196
### Callback *with* Arguments
193
197
194
-
Executing callbacks with arguments can be tricky. The below example illustrates how to think about callbacks with arguments:
This will not work because it calls `myCallBack( param1, param2 )` and then passes the return value as the second parameter to [$.get()](http://api.jquery.com/jQuery.get/).
207
+
The reason this fails is that the code executes `myCallBack( param1, param2 )` immediately
208
+
and then passes the myCallBack's *return value* as the second parameter to `$.get`.
209
+
We actually want to pass in `myCallBack` the function, not `myCallBack`'s return value
210
+
(which might or might not be a function). So, how to pass in `myCallBack`*and* include its arguments?
205
211
206
212
#### Right
207
213
208
-
The problem with the above example is that `myCallBack( param1, param2 )` is
209
-
evaluated before being passed as a function. Javascript (and by extension, jQuery)
210
-
expects a function pointer in cases like these, e.g., `setTimeout( function() {}, 100)`.
211
-
212
-
In the example below, an anonymous function is created (just a block of
213
-
statements) and is registered as the callback function. Note the use of
214
-
`function() {`. The anonymous function does exactly one thing: calls
215
-
`myCallBack`, with the values of `param1` and `param2` from the outer scope.
214
+
To defer executing `myCallBack` with its parameters, you can use an anonymous function as a wrapper.
215
+
Note the use of `function() {`. The anonymous function does exactly one thing: calls
216
+
`myCallBack`, with the values of `param1` and `param2`.
216
217
217
218
```
218
219
$.get( "myhtmlpage.html", function() {
@@ -222,4 +223,5 @@ statements) and is registered as the callback function. Note the use of
222
223
});
223
224
```
224
225
225
-
`param1` and `param2` are evaluated as a callback when the `$.get` is done getting the page.
226
+
When `$.get` finishes getting the page `myhtmlpage.html`, it executes the anonymous function,
0 commit comments