forked from khannotations/javascript-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
236 lines (222 loc) · 13.7 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<html>
<head>
<title>Learing javascript</title>
<link rel="stylesheet" href="stylesheets/style.css" type="text/css"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="scripts/main.js" type="text/javascript"></script>
</head>
<body>
<div class="container">
<div id="content">
<div id="1" class="page">
<h1>Learning Javascript</h1>
<p>
Today, we learned the basic syntax for <strong>Javascript</strong>, the language used to <strong>animate</strong> the web. There are slides available, but most of what we did isn't on them. I'll do a brief summary here.
</p>
<p><strong>It's very important that you type all the lines of code, not copy paste!</strong> You learn best by doing, not by reading. The more you type, the more you'll retain, I promise.
</p>
<p class="note">To run the code in this tutorial, you'll need to use Chrome's console (Command-Option-j on a Mac). If you're not using Chrome, quickly download it <a href="https://www.google.com/intl/en/chrome/browser/" target="_blank">here</a>.
<p>
The astute will realize that the navigation of this tutorial is done with Javascript (notice that the URL never changes!). If you're curious, view the source and the script by looking at Resources tab in Chrome's Inspector (View -> Developer -> Inspect, click the 2nd tab, the arrow next to "index.html", then the arrow next to "scripts", then "main.js"), or just by opening it from the Day 5 folder in Sublime. <strong>You'll be able to understand most of it by the end of these 8 pages!</strong>
</p>
</div>
<div id="2" class="page">
<h1>Basic syntax</h1>
<p>A <strong>variable</strong> is exactly what it was in algebra: something in which to store data. In javascript, we declare and store variables like this:</p>
<code>
var a = 7; <span class="comment">// anything after 2 /'s (on the same line) is a <strong> comment </strong>and won't be executed.</span><br/>
<span class="comment">// so you can literally type anything!! a;lk ajd!</span>
</code>
<p>
The keyword <code class="inline">var</code> tells the browser to make a new variable called <code class="inline"> a</code>. The <code class="inline"> =</code> then tells the browser to set it to the value 7. Finally, the <code class="inline">;</code> tells the browser that the line of code is over -- <strong>All statements should end in semicolons!!</strong>
</p>
<p>
Then how come it returns <code class="inline"> undefined</code>? We'll get to that later. You can check what any variable is just by typing it:
</p>
<code>
a <br/>
<span class="comment"> // => 7 </span>
</code>
<p class="note">I'll use the <code class="inline"><span class="comment">// =></span></code> to denote the <strong>output</strong> of a line of code -- no need to type it (or any other comments, for that matter) in.
</p>
</div>
<div id="3" class="page">
<h1>Types</h1>
<p>
There are (many) different types of variables. We'll learn about 3 today. A <strong>type</strong> tells the engine what kind of data a variable holds. For example, try this:
</p>
<code>
a = 7; <span class="comment">// We don't have to type 'var' again because the engine already knows a exists; var is for only the <strong>first time</strong> you declare a variable</strong></span><br>
var b = "7"; <br/>
a + b; <br/>
<span class="comment"> // => What will it be?</span>
</code>
<p>
You may be surprised to see it returned <code class="inline"> "77"</code>. That's because a is of type <strong>number</strong> and b is of type <strong>string</strong>, or a collection of letters (called <code class="inline"> chars</code> in comp sci talk). When you add two strings, you just concatenate them, as you'd expect (so "java"+"script" == "javascript"). The engine treated <code class="inline"> a</code> like a string so that the addition could make sense. How else would you add 7 to, say, "cat"?
</p>
<p>To check a variable's type:</p>
<code>
typeof a <span class="comment"> // => "number" </span><br/>
typeof b <span class="comment"> // => "string" </span><br/>
</code>
</div>
<div id="4" class="page">
<h1>Functions</h1>
<p>
<strong>Functions</strong> are also exactly what they were in algebra: something that takes input and gives output, and if the input is the same, the output is too. We write functions like this:
</p>
<p class="note">To type a new line in the console, use <strong>shift + enter</strong></p>
<code>
function (a, b) { <br/>
<strong>return</strong> a + b; <br/>
}<br/>
</code>
<p>
This is correct syntax, but won't work in the console (something about an unexpected '('). This is just because the console doesn't like multiple line statements, and we'll fix it in a minute, but first let's analyze what we typed. <code class="inline"> function</code> tells the browser that we're declaring a function. Everything enclosed in <code class="inline"> {}</code> is a part of the function: they tell the engine where the function starts and ends. <code class="inline"> (a, b)</code> defines the <strong>input</strong> -- in this function, we're taking two pieces of input, called <code class="inline"> a</code> and <code class="inline"> b</code>. Finally, the <code class="inline"> return</code> tells the function what to <strong> output</strong>, in this case, the sum of a and b.
</p>
</div>
<div id="5" class="page">
<h1>Making it work</h1>
<p>To make the code work, type this:</p>
<code>
var func = function (a, b) { <br/>
<strong>return</strong> a + b; <br/>
}<br/>
</code>
<p>
This is cool! We've just assigned the a function to the variable <code class="inline"> func</code>. To call (i.e. use) the function we use a similar syntax:
</p>
<code>
func(6, 7); <span class="comment"> // => 13 </span> <br/>
func; <span class="comment"> // => function (a, b) { return a + b; } </span>
</code>
<p>
Notice the difference between checking what the function is (the second line) and using it. Now, what do you think you'd get by typing:
</p>
<code>
typeof func;<span class="comment">// ?</span><br/>
<span class="comment">// How about this one:</span><br/>
alert("howdy soldier!");
</code>
<p>
<code class="inline">alert</code> is that pesky function that some sites use to show messages. Now you know how to piss people off with javascript!</p>
<p>
Notice that javascript doesn't make you specify the <strong>types of input</strong>. That is, this will work too:
</p>
<code>
func("cat", "dog"); <span class="comment">// Love that show.</span><br/>
alert(17);<br/>
alert(func);<br/>
alert(func("cat", "dog")); <span class="comment">// Curveball! What will this do?
</code>
<p>
The last line is an example of a <strong>nested</strong> function. The engine evaluates the inner function, then uses its return value as the input to the outer function. Just like using <strong>f(g(x))</strong> back in Algebra!
</p>
</div>
<div id="6" class="page">
<h1>JS and the DOM</h1>
<p>You may be thinking, this is all very cool, but what does this have to do with the web? Well, Javascript has a special syntax for interacting with objects on the web page. Open up the console while viewing this page and type
</p>
<code>
document.get
</code>
<p>
Chrome should autofill with a lot of options, like <code class="inline"> getElementById</code> and <code class="inline"> getElementsByClassName</code>. Pick one and see what it does -- at this point, it should be pretty self explanatory. For example:
</p>
<code>
document.getElementById("6");<br/>
<span class="comment"> // => All the code for this page </span>
</code>
<p>
Now what will the following do? Try it!
</p>
<code>
document.getElementById(6).style.color = "blue"; <br/>
document.getElementById(6).innerHTML = "javascript rocks";
</code>
<p class="note">To undo changes, all you have to do is refresh the page -- changes made with javascript don't <strong>persist</strong>, that is, they never change any of the original code that made a web page!</p>
<p>If the code isn't self-explanatory, don't worry: you will never again use it, because <strong>we have jQuery</strong>.
</p>
</div>
<div id="7" class="page">
<p>The above javascript is pretty cool, but it involves <strong>a lot of typing</strong>. And what if I didn't want to get <code class="inline"> #6</code> but something more complicated, like <code class="inline"> #footer a.some_class</code>? Then things get tough.
</p>
<h1>Enter jQuery</h1>
<p>jQuery makes all this easy. Instead of the annoying <code class="inline"> getElementById</code>, we can just use <strong>CSS selectors</strong> to get elements. Try:
</p>
<code>
$("#7"); <span class="comment"> // The same thing as document.getElementById("7") -- so much less typing! </span><br/>
$("code").css("color", "blue"); <br/>
$(".nav").html("javascript rocks"); <span class="comment"> // Check the next and previous buttons! </span><br/>
</code>
<p>
This is why we love jQuery, and it's <strong>essential</strong> that you know it as a web developer. No one (literally, no one) works in plain javascript anymore: everyone uses jQuery. Much, much more can be found at <a href="http://jquery.com" target="_blank">jQuery.com</a>
</p>
<h1>I wanna do it myself!</h1>
<p>I'd hope so. Unfortunately, jQuery doesn't come built-in with the browser like javascript does. If you look at this page's source, you'll notice this in the head:
</p>
<code>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script><br/>
</code>
<p>
All you need to do is copy that into your own pages, and you have jQuery! To write javascript files (you don't want to be typing into the console all the time), make that <code class="inline"> scripts</code> folder in your project that we always talk about and never use, and create a main.js file in there.
</p>
</div>
<div id="8" class="page">
<h1>A working example</h1>
<p>Here is the example we did from class yesterday. First create a folder for the project (i.e. Day 5). In it, make an index.html. Make a stylesheets and scripts directory as usual. In the scripts folder, make a main.js.
</p>
<code>
<span class="comment"> // index.html </span><br/>
<!doctype html><br/>
<html><br/>
<head><br/>
<span class="comment"><!-- include jQuery -- ></span><Br/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script><br/>
<span class="comment"><!-- THEN include your own script --></span><br/>
<script src="scripts/main.js"></script><br/>
<link rel="stylesheet" href="stylesheets/style.css"><br/>
</head><br/>
<body><br/>
<p id="test">hey</p><br/>
</body><br/>
</html><br/>
</code>
<p>Some very basic stuff. Notice that we include jQuery <strong>first</strong>, then our main.js script. Now for the kicker:</p>
<code>
<span class="comment"> // scripts/main.js </span><br/>
$(document).ready(function() { <span class="comment"> // We'll explain this line later </span> <br/>
console.log($("#test"));<br/>
$("p").animate({fontSize: "30px"}, 1000);<br/>
});<span class="comment"> // always remember to close your {}'s and ()'s!</span><br/>
<span class="comment"> // We'll go over what the {fontSize..} means later as well.
</code>
<p>
And that's it! Open up index.html in a browser and watch that text grow (the <code class="inline">1000</code> is the number of <strong>milliseconds</strong> the animation will take). In JavaScript, time is always in milleseconds.
</p>
<p>
Now you can use jQuery, CSS and HTML -- you <strong>know</strong> everything this class has to teach. All that's left is review, learning more functions and practice!
</p>
<p>
As always, email or call with questions!
</p>
</div>
</div>
</div>
<div id="nav">
<div id="prev" class="nav"><- Previous</div>
<div id="fake_prev" style="width:20%;" ></div>
<div id="info">
<div class="page-nav" target="1">1</div>
<div class="page-nav" target="2">2</div>
<div class="page-nav" target="3">3</div>
<div class="page-nav" target="4">4</div>
<div class="page-nav" target="5">5</div>
<div class="page-nav" target="6">6</div>
<div class="page-nav" target="7">7</div>
<div class="page-nav" target="8">8</div>
</div>
<div id="next" class="nav">Next -></div>
</div>
</body>
</html>