Skip to content

Commit 31b5af1

Browse files
committed
move objects (before?)
1 parent 480e69b commit 31b5af1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+445
-362
lines changed

1-js/2-first-steps/02-external-script/1-hello-alert-ext/alert.js renamed to 1-js/2-first-steps/01-hello-world/2-hello-alert-ext/alert.js

File renamed without changes.

1-js/2-first-steps/02-external-script/1-hello-alert-ext/index.html renamed to 1-js/2-first-steps/01-hello-world/2-hello-alert-ext/index.html

File renamed without changes.

1-js/2-first-steps/02-external-script/1-hello-alert-ext/solution.md renamed to 1-js/2-first-steps/01-hello-world/2-hello-alert-ext/solution.md

File renamed without changes.

1-js/2-first-steps/02-external-script/1-hello-alert-ext/task.md renamed to 1-js/2-first-steps/01-hello-world/2-hello-alert-ext/task.md

File renamed without changes.

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

Lines changed: 67 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,8 @@ So we'll start with attaching a script to the webpage. For other environments li
88

99
[cut]
1010

11-
[todo remove defer/async from here and move to 2nd part?]
12-
1311
## The "script" tag
1412

15-
[todo need this? and special (need it too?)]
16-
```smart header="What if I want to move faster?"
17-
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.
18-
19-
If you have enough time and want to learn things in details then read on :)
20-
```
21-
2213
JavaScript programs can be inserted in any place of HTML with the help of the `<script>` tag.
2314

2415
For instance:
@@ -50,17 +41,6 @@ You can run the example clicking on a "Play" button in it's right-top corner.
5041

5142
The `<script>` tag contains JavaScript code which is automatically executed when the browser meets the tag.
5243

53-
Please note the execution sequence:
54-
55-
1. Browser starts to parse the document and display the page.
56-
2. When the browser meets `<script>`, it switches to the JavaScript execution mode. In this mode it executes the script.
57-
3. The `alert` command shows a message and pauses the execution.
58-
4. Note that at this moment a part of the page before the script is shown already.
59-
5. When the script is finished, it gets back to the HTML-mode, and *only then* it shows the rest of the document.
60-
61-
![Rendering order](hello-world-render.png)
62-
63-
A visitor won't see the content after the script until it is executed. In other words, a `<script>` tag blocks rendering.
6444

6545
## The modern markup
6646

@@ -82,11 +62,76 @@ Comments before and after scripts.
8262
//--></script>
8363
```
8464

85-
These comments were supposed to hide the code from an old browser that did't understand a `<script>` tag. But for all browsers born in the past 15+ years that's not an issue. I only mention it here, because it serves as a pointer. If you see that in a code somewhere -- it's probably really old and probably not worth looking into.
65+
These comments were supposed to hide the code from an old browser that did't know about a `<script>` tag. But all browsers born in the past 15+ years don't have any issues. It is only mentioned here, because it serves as a sign. If you see that somewhere -- that code is probably really old and not worth looking into.
66+
67+
68+
## External scripts
69+
70+
If we have a lot of JavaScript code, we can it put it into a separate file.
71+
72+
The script file is attached to HTML like this:
73+
74+
```html
75+
<script src="/path/to/script.js"></script>
76+
```
77+
78+
Here `/path/to/script.js` is an absolute path to the file with the script (from the site root).
79+
80+
It is also possible to provide a path relative to the current page. For instance, `src="script.js"` would mean a file `"script.js"` from the current folder.
81+
82+
We can give a full URL al well, for instance:
83+
84+
```html
85+
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.2.0/lodash.js"></script>
86+
```
87+
88+
To attach several scripts, use multiple tags:
89+
90+
```html
91+
<script src="/js/script1.js"></script>
92+
<script src="/js/script2.js"></script>
93+
94+
```
95+
96+
```smart
97+
As a rule, only simplest scripts are put into HTML. More complex ones reside in separate files.
98+
99+
The benefit of a separate file is that the browser will download it and then store in its [cache](https://en.wikipedia.org/wiki/Web_cache).
100+
101+
After it, other pages which want the same script will take it from the cache instead of downloading it. So the file is actually downloaded only once.
102+
103+
That saves traffic and makes pages faster.
104+
```
105+
106+
````warn header="If `src` is set, the script content is ignored."
107+
A single `<script>` tag may not have both an `src` and the code inside.
108+
109+
This won't work:
110+
111+
```html
112+
<script *!*src*/!*="file.js">
113+
alert(1); // the content is ignored, because src is set
114+
</script>
115+
```
116+
117+
We must choose: either it's an external `<script src="…">` or a regular `<script>` with code.
118+
119+
The example above can be split into two scripts to work:
120+
121+
```html
122+
<script src="file.js"></script>
123+
<script>
124+
alert(1);
125+
</script>
126+
```
127+
````
86128
87129
## Summary
88130
89131
- We can use a `<script>` tag to add JavaScript code to the page.
90132
- The `type` and `language` attributes are not required.
91-
- A `<script>` tag blocks page rendering. Later we'll see how to evade that where needed.
133+
- Scripts in an external file can be inserted on the page via `<script src="path"></script>`.
134+
135+
136+
There is much more about browser scripts and their interaction with the web-page. But let's keep in mind that this part of the tutorial is devoted to Javascript language. So we shouldn't distract ourselves from it. We'll be using a browser as a way to run Javascript, very convenient for online reading, but yet one of many.
92137
339 Bytes
Loading
628 Bytes
Loading

1-js/2-first-steps/03-structure/article.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ alert( 'Hello' );
2525
alert( 'World' );
2626
```
2727

28-
## The semicolon [#semicolon]
28+
## Semicolons [#semicolon]
2929

30-
The semicolon may be omitted in most cases when a line break exists.
30+
A semicolon may be omitted in most cases when a line break exists.
3131

3232
This would also work:
3333

@@ -86,11 +86,10 @@ The error in the no-semicolon variant occurs because JavaScript engine does not
8686
alert( "There will be an error" )[1, 2].forEach(alert)
8787
```
8888
89-
And in this particular case, that's just wrong. There must be two independent statements. Hence the error.
90-
89+
And in this particular case, that's just wrong, hence the error. There are other situations when such thing happens.
9190
````
9291

93-
It's recommended to put semicolons between statements even if they are separated by newlines. This rule is widely adopted by the community. Let's note once again -- it is possible to leave out semicolons most of time. But it's safer, especially for a beginner -- to put them.
92+
It's recommended to put semicolons between statements even if they are separated by newlines. This rule is widely adopted by the community. Let's note once again -- *it is possible* to leave out semicolons most of time. But it's safer, especially for a beginner -- to put them.
9493

9594
## Comments
9695

1-js/2-first-steps/04-strict-mode/article.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ Only comments may appear above `"use strict"`.
4848
4949
5050
```smart header="`use strict` for functions"
51-
We will learn [functions](/function-basics) very soon.
51+
We will learn functions (a way to group commands) soon.
5252
53-
Looking ahead let's just note that `"use strict"` can be put at the start of a function instead of the whole script. Then the strict mode is enabled in this function only. But that limitation is exceptionally rarely needed.
53+
Looking ahead let's just note that `"use strict"` can be put at the start of a function instead of the whole script. Then the strict mode is enabled in that function only. But usually people put it on top of scripts.
5454
```
5555
5656

1-js/2-first-steps/05-variables/article.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Variables are used to store the information.
1010

1111
## A variable
1212

13-
A [variable]("https://en.wikipedia.org/wiki/Variable_(computer_science)") is a basic "named storage" for the information. We can use variables to store the goods, visitors etc.
13+
A [variable]("https://en.wikipedia.org/wiki/Variable_(computer_science)") is a "named storage" for the information. We can use variables to store goodies, visitors and other data.
1414

1515
To create a variable in JavaScript, we need to use the `let` keyword.
1616

@@ -72,12 +72,12 @@ let user = 'John',
7272
message = 'Hello';
7373
```
7474

75-
...Or even in comma-first style:
75+
...Or even in the "comma-first" style:
7676

7777
```js no-beautify
7878
let user = 'John'
79-
,age = 25
80-
,message = 'Hello';
79+
, age = 25
80+
, message = 'Hello';
8181
```
8282

8383
Technically, all these variants do the same. So, it's a matter of personal taste and aestetics.

0 commit comments

Comments
 (0)