Skip to content

Commit b5438da

Browse files
committed
works
1 parent 26728a7 commit b5438da

File tree

32 files changed

+544
-290
lines changed

32 files changed

+544
-290
lines changed

1-js/1-getting-started/1-intro/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ The examples of such restrictions are:
6868
Modern browsers allow it to work with files, but the access is limited and only provided if the user does certain actions, like "dropping" a file into a browser window or selecting it via an `<input>` tag.
6969

7070
There are ways to interact with camera/microphone and other devices, but they require an explicit user's permission. So a JavaScript-enabled page may not sneakily enable a web-camera, observe the surroundings and send the information to the [NSA](https://en.wikipedia.org/wiki/National_Security_Agency).
71-
- Different tabs/windows generally do not know about each other. Sometimes they do, for example when one window uses JavaScript to open the other one. Such action is allowed. But even in this case, JavaScript from one page may not access the other if they compe from different sites (from a different domain, protocol or port).
71+
- Different tabs/windows generally do not know about each other. Sometimes they do, for example when one window uses JavaScript to open the other one. But even in this case, JavaScript from one page may not access the other if they compe from different sites (from a different domain, protocol or port).
7272

7373
That is called a "Same Origin Policy". To workaround that, *both pages* must contain a special JavaScript code that handles data exchange.
7474

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Code editors
2+
3+
The code editor is the place where a programmer spends most his time.
4+
5+
There are two archetypes: IDE and lightweight editors. Many people feel comfortable choosing one tool of each type.
6+
7+
[cut]
8+
9+
## IDE
10+
11+
The term [IDE](https://en.wikipedia.org/wiki/Integrated_development_environment) (Integrated Development Environment) is a powerful editor with many additional features that usually operates on a "whole project".
12+
13+
An IDE loads the project (can be many files), and then allows navigate between files, provides autocompletion based on the whole project, does other "project-level" stuff.
14+
15+
If you haven't considered selecting an IDE, pleae look at the following variants:
16+
17+
- IntelliJ editors: [WebStorm](http://www.jetbrains.com/webstorm/) for frontend development and [PHPStorm (PHP)](http://www.jetbrains.com/phpstorm/), [IDEA (Java)](http://www.jetbrains.com/idea/), [RubyMine (Ruby)](http://www.jetbrains.com/ruby/) and other if you need additional languages.
18+
- Visual Studio is fine if you're a .NET developer.
19+
- Eclipse-based products, like [Aptana](http://www.aptana.com/) and Zend Studio.
20+
- [Komodo IDE](http://www.activestate.com/komodo-ide) and it's lightweight free version [Komodo Edit](http://www.activestate.com/komodo-edit).
21+
- [Netbeans](http://netbeans.org/)
22+
23+
All of them with the exception of Visual Studio are cross-platform.
24+
25+
Most IDEs are paid, but have a trial period. Their cost is usually negligible compared to a qualified developer's salary, so just choose the best one for you.
26+
27+
## Lightweight editors
28+
29+
"Lightweight editors" are not as powerful as IDEs, but they're fast, elegant and simple.
30+
31+
They are mainly used to instantly open and edit a file.
32+
33+
The main difference between a "lightweight editor" and an "IDE" is that the latter works on a project-level, meaning it has to load a lot of data to start, and the former one opens just the files. That's much faster.
34+
35+
In practice, "lightweight" editors may have a lot of plugins including directory-level syntax analyzers and autocompleters, so there's no strict border between a lightweight editor and an IDE.
36+
37+
The following options deserve your attention:
38+
39+
- [Sublime Text](http://www.sublimetext.com) (cross-platform, shareware).
40+
- [Atom](https://atom.io/) (cross-platform, free).
41+
- [Notepad++](http://sourceforge.net/projects/notepad-plus/) (Windows, free).
42+
- Vim, Emacs are cool. If you know how to use them.
43+
44+
## My favorites
45+
46+
I believe one should have both an IDE for projects and a lightweight editor for quick and easy file editing.
47+
48+
I'm using:
49+
50+
- [WebStorm](http://www.jetbrains.com/webstorm/) for JS, and if there is one more language in the project, then I switch to other Jetbrains editors like [PHPStorm](http://www.jetbrains.com/phpstorm/) (PHP), [IDEA](http://www.jetbrains.com/idea/) (Java), [RubyMine](http://www.jetbrains.com/ruby/) (Ruby). There are editors for other languages too, but I didn't use them.
51+
- As a lightweight editor -- [Sublime Text](http://www.sublimetext.com).
52+
53+
If you don't know what to choose -- you can consider these ones.
54+
55+
## Let's not argue
56+
57+
The editors in the lists above are those that me or my friends -- good developers use for a long time and are happy with.
58+
59+
There are other great editors in our big world, please choose the one you like the most.
60+
61+
The choice of an editor, like any other tool, is individual and depends on your projects, habbits, personal preferences.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Developer console
2+
3+
And the one more thing before we get down to coding.
4+
5+
A code is error-prone. You are quite likely to have errors... Oh what I'm talking? You are *absolutely* going to make errors, at least if you're a human, not a [robot](https://en.wikipedia.org/wiki/Bender_(Futurama)).
6+
7+
But in the browser, a user doesn't see the errors by default. So, if something goes wrong in the script, we won't see what's broken and can't fix it.
8+
9+
To see errors and get a lot of other useful information about scripts, browsers have embedded "developer tools".
10+
11+
**It is recommended to use Chrome or Firefox for the development.**
12+
13+
Other browsers also provide developer tools, but are usually in a "catching-up" position, compared to Chrome/Firefox which are the best.
14+
15+
If there is an error in the certain browser only, then we can use it's developer tools, but usually -- Chrome/Firefox.
16+
17+
Developer tools are really powerful, there are many features. On this stage let's just look how to open them, look at errors and run JavaScript commands.
18+
19+
[cut]
20+
21+
## Google Chrome
22+
23+
Open the page [bug.html](bug.html).
24+
25+
There's an error in the JavaScript code on it. It's hidden from a regular visitor's eyes, so let's open developer tools to see it.
26+
27+
Press the key `key:F12` or, if you're on Mac, then `key:Cmd+Opt+J`.
28+
29+
The developer tools will open on the Console tab by default.
30+
31+
It looks somewhat like this:
32+
33+
![chrome](chrome.png)
34+
35+
The exact look depends on your Chrome version. It changes from time to time, but should be similar.
36+
37+
- Here we can see the red-colored error message. In this case the script contains a "lalala" command, which was put there just because it is unknown.
38+
- On the right, there is a clickable link to the source `bug.html:12` with the line number where the error has occured.
39+
40+
Below the error message there is a blue `>` symbol. It marks a "command line" where we can type JavaScript commands and press enter to run them (`key:Shift+Enter` to input multiline commands).
41+
42+
Now we can see errors and that's enough for the start. We'll be back to developer tools later and cover debugging more in-depth in the chapter <info:debugging-chrome>.
43+
44+
45+
## Firefox, Edge and others
46+
47+
Most other browsers use `key:F12` to open developer tools.
48+
49+
The look & feel of them is quite similar, once we know how to use one of them (can start with Chrome), can easily switch to another.
50+
51+
## Safari
52+
53+
Safari (if you use Mac, not Windows/Linux) is a little bit special here. We need to enable the "Develop menu" first.
54+
55+
There's a checkbox for that at the bottom of the "Advanced" pane of the preferences:
56+
57+
![safari](safari.png)
58+
59+
Now `key:Cmd+Opt+C` can toggle the console. Also note that the new top menu item has appeared with many useful options.
60+
61+
## Summary
62+
63+
- Developer tools allow us to see errors, run commands, examine variables and much more.
64+
- They can be opened with `key:F12` for most browsers under Windows. Chrome for Mac needs `key:Cmd+Opt+J`, Safari: `key:Cmd+Opt+C` (need to enable first).
65+
66+
Now we have the environment ready. In the next section we get down to JavaScript.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
4+
<head>
5+
<meta charset="utf-8">
6+
</head>
7+
8+
<body>
9+
10+
There is an error in the script on this page.
11+
<script>
12+
lalala
13+
</script>
14+
15+
</body>
16+
17+
</html>
74.5 KB
Loading
67.8 KB
Loading
105 KB
Loading
285 KB
Loading

1-js/2-first-steps/01-hello-world/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ In this chapter we'll create a simple script and see it working.
77
## The "script" tag
88

99
```smart header="What if I want to move faster?"
10-
In the case if the reader has developed with JavaScript already or has a lot of experience in another language, he can skip detailed explanatins and jump to <info:javascript-specials>. There he can find an essense of important features.
10+
In the case if you've developed with JavaScript already or have a lot of experience in another language, you can skip detailed explanatins and jump to <info:javascript-specials>. There you can find an essense of important features.
1111
1212
If you have enough time and want to learn things in details then read on :)
1313
```

1-js/2-first-steps/02-external-script/article.md

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ For example, in the code below -- until all rabbits are counted, the bottom `<p>
9090
9191
The behavior is called "synchronous". Usually it causes no problems, but there's an important consequence.
9292
93-
**If the script is external, then until the browser executes it, it can't show the page below.**
93+
**If a script is external, then until the browser downloads and executes it, a visitor has to wait.**
9494
9595
So, in this document, until `big.js` loads and executes, the `<body>` content is hidden:
9696
@@ -107,21 +107,22 @@ So, in this document, until `big.js` loads and executes, the `<body>` content is
107107
</html>
108108
```
109109
110-
The question is -- do we really want to hide the body until the script finishes?
110+
The question is -- do we really want the visitor to wait until the script finishes?
111111
112112
Most of time, we don't.
113113
114114
Sometimes, a script may contain a very important code that really must be loaded before the rest of the page is parsed (and the scripts below executed). But that's only sometimes.
115115
116-
Usually a visitor should see the page content while the script is loading.
116+
Usually a visitor should be able to see the page while the script is loading. That's especially true for sites with an important content (like the tutorial) -- even if some interfaces are yet inactive (scripts not loaded yet), the visitor can read the text and navigate.
117117
118118
There are situations when such blocking is even dangerous. For example, when we attach a script from the banner system, or a 3rd-party integration code.
119119
120120
Like this:
121121
122122
```html run height=100
123-
Wait. The text belown will shown up only after the script executes.
123+
Wait. The text below will shown up only after the script executes.
124124
125+
<!-- this banner script takes time to load -->
125126
<script src="/article/external-script/banner.js?speed=0"></script>
126127
127128
<p>…Important information!</p>
@@ -135,8 +136,8 @@ Our first attempt could be to put all such scripts to the bottom of the `<body>`
135136
136137
But the solution is not perfect:
137138
138-
1. The script won't start loading until the whole page loads. If the page is large, then the delay may be significant. We'd like the browser to start loading a script early, but still do not block the page.
139-
2. If there is more than one script at the bottom of the page, and the first script is slow, then the second one will have to wait for it. Browser executes only one `<script>` tag in one moment. So scripts queue one after another. That's not always welcome: ads and web analytics scripts should run immediately as they load.
139+
1. The script won't start loading until the HTML loads. If the HTML is large, then the delay may be significant. We'd like the browser to start loading a script early, but still do not block the page.
140+
2. If there are many scripts at the bottom of the page, then they queue up. Browser executes only one `<script>` tag in one moment. Please note that it tries to downloads scripts in parallel (for better network utilization), but the execution order is still strictly one after another. So, the first script can block the rest if it's slow. That's not always welcome: some scripts, like ads and web analytics scripts should run independantly and immediately as they load, not block each other.
140141
141142
And here come the attributes `async` and `defer`.
142143
@@ -147,38 +148,27 @@ The `defer` attribute.
147148
: The script with `defer` also executes asynchronously, like async. But there are two essential differences:
148149
149150
1. The browser guarantees to keep the relative order of "deferred" scripts.
150-
2. A "deferred" script always executes after HTML-document is fully loaded.
151-
152-
We'll discuss them more in-depth further in this chapter.
153-
154-
```smart header="Either `async` or `defer`"
155-
We can't use both `defer` and `async` on a single script. If we do that, `async` takes precedence, `defer` is ignored.
156-
```
151+
2. A "deferred" script always executes after the HTML-document is fully loaded.
157152
158-
```warn header="Attributes `async/defer` are only for external scripts"
159-
The attributes `async/defer` work only when set on a script with `src`.
160-
161-
On a script without `src` like <code>&lt;script&gt;...&lt;/script&gt;</code>, they will be ignored.
162-
```
163153
164154
Let's modify the "blocking script" example that we've seen before, adding `async`:
165155
166156
```html run height=100
167157
Wait. The text belown will shown up only after the script executes.
168158
159+
<!-- This banner script take some time to load... But nobody cares. -->
169160
<script *!*async*/!* src="/article/external-script/banner.js?speed=0"></script>
170161
171162
<p>…Important information!</p>
172163
```
173164
174165
Now if we run it, we'll see that the whole document is displayed immediately, and the external script runs when it loads.
175166
176-
## Defer vs Async
167+
Let's see more examples with `defer` and `async` to clearly understand the similarities and the differences.
177168
178-
Please note that there is one similarity and two differences between `defer` and `async`.
169+
Both attributes allow the browser to show the page without waiting for the script to load. But...
179170
180-
1. **Both attributes allow the browser to show the page without waiting for the script to load.**
181-
2. **Deferred scripts keep the relative order, while async scripts do not.**
171+
1. Deferred scripts keep the relative order, while async scripts do not.
182172
183173
For example, in the code below (with `async`) there are two scripts. The one which loads first will run first.
184174
@@ -198,7 +188,7 @@ Please note that there is one similarity and two differences between `defer` and
198188
199189
This feature of "deferred" scripts is important when `2.js` relies on the result of `1.js` and we must be sure that the order is determined.
200190
201-
3. **A script with `defer` always waits for the HTML-document to fully load. The `async` script runs immediately as it loads.**
191+
2. A script with `defer` always waits for the HTML-document to fully load. The `async` script runs immediately as it loads.
202192
203193
For instance, when the document is large, like:
204194
@@ -212,25 +202,35 @@ Please note that there is one similarity and two differences between `defer` and
212202
213203
...Here `async.js` executes when it loads -- possibly, before the document is fully loaded. In contrast, `defer.js` always waits for the full document to be ready.
214204
215-
The choice between `defer` and `async` here depends on our intentions. Sometimes a script doesn't need the document at all (like a web counter), so it should execute ASAP. In this case `async` is superb.
205+
So, if a script doesn't need the rest of the document (like a web counter), then `async` is superb. And in another case a script may need the whole document to do some work with it. Then `defer` is preferable.
206+
207+
208+
```smart header="Either `async` or `defer`"
209+
We can't use both `defer` and `async` on a single script. If we do that, `async` takes precedence, `defer` is ignored.
210+
```
211+
212+
```warn header="Attributes `async/defer` are only for external scripts"
213+
The attributes `async/defer` work only when set on a script with `src`.
214+
215+
On a script without `src` like <code>&lt;script&gt;...&lt;/script&gt;</code>, they will be ignored.
216+
```
216217
217-
And in another case a script may need the whole document to do some work with it. Then `defer` is preferable.
218218
219219
## Summary
220220
221221
- Scripts in an external file can be inserted on the page via `<script src="path"></script>`.
222-
- Normally, the browser doesn't show the document after the script until it executes. Unless the script has `async` or `defer` attributes.
222+
- The browser doesn't show the content below the script until it executes. Unless the script has `async` or `defer` attributes.
223223
- Both `async` and `defer` allow the browser to start script loading and then continue to parse/show the page. They only work on external scripts.
224224
- The difference is that `defer` keeps the relative script order and always executes after the document is fully loaded. In contrast, `async` script executes when it loads, without any conditions.
225225
226226
Before inserting an external `<script src="…">` tag, we should always consider the side-effect of blocking the page rendering. Especially if it's a 3rd-party script. And if we don't want that, then `defer/async` can come in handy.
227227
228228
````smart header="Running ahead..."
229-
For an advanced reader who knows that new tags can be added on page dynamically, we'd like to note that dynamic `<script>` tags behave as `async`. In other words, they run as they load.
229+
For an advanced reader who already knows how to add new tags on the page using DOM: dynamic `<script>` tags behave as `async`. In other words, they run as they load, independantly.
230230
231-
But we can change that. If we set `script.async = false`, then such scripts are run in the load of insertion.
231+
But we can adjust that. If we set `script.async = false`, then such scripts are run in the load of insertion. That's a way to keep the relative order for dynamically added tags.
232232
233-
The code example:
233+
For example:
234234
```js
235235
function addScript(src){
236236
let script = document.createElement('script');
@@ -246,6 +246,6 @@ addScript('2.js'); // but execute in the order of insertion
246246
addScript('3.js'); // that is: 1 -> 2 -> 3
247247
```
248248
249-
We'll cover page dynamic tags and page manipulation in detail later, in the second part of the tutorial.
249+
We'll surely cover dynamic tags and page manipulation in detail later, in the second part of the tutorial.
250250
````
251251

0 commit comments

Comments
 (0)