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: 1-js/2-first-steps/01-hello-world/article.md
+67-22Lines changed: 67 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,17 +8,8 @@ So we'll start with attaching a script to the webpage. For other environments li
8
8
9
9
[cut]
10
10
11
-
[todo remove defer/async from here and move to 2nd part?]
12
-
13
11
## The "script" tag
14
12
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
-
22
13
JavaScript programs can be inserted in any place of HTML with the help of the `<script>` tag.
23
14
24
15
For instance:
@@ -50,17 +41,6 @@ You can run the example clicking on a "Play" button in it's right-top corner.
50
41
51
42
The `<script>` tag contains JavaScript code which is automatically executed when the browser meets the tag.
52
43
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
-

62
-
63
-
A visitor won't see the content after the script until it is executed. In other words, a `<script>` tag blocks rendering.
64
44
65
45
## The modern markup
66
46
@@ -82,11 +62,76 @@ Comments before and after scripts.
82
62
//--></script>
83
63
```
84
64
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
+
<scriptsrc="/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.
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
+
<scriptsrc="file.js"></script>
123
+
<script>
124
+
alert(1);
125
+
</script>
126
+
```
127
+
````
86
128
87
129
## Summary
88
130
89
131
- We can use a `<script>` tag to add JavaScript code to the page.
90
132
- 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.
Copy file name to clipboardExpand all lines: 1-js/2-first-steps/03-structure/article.md
+4-5Lines changed: 4 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,9 +25,9 @@ alert( 'Hello' );
25
25
alert( 'World' );
26
26
```
27
27
28
-
## The semicolon[#semicolon]
28
+
## Semicolons[#semicolon]
29
29
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.
31
31
32
32
This would also work:
33
33
@@ -86,11 +86,10 @@ The error in the no-semicolon variant occurs because JavaScript engine does not
86
86
alert( "There will be an error" )[1, 2].forEach(alert)
87
87
```
88
88
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.
91
90
````
92
91
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.
Copy file name to clipboardExpand all lines: 1-js/2-first-steps/04-strict-mode/article.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -48,9 +48,9 @@ Only comments may appear above `"use strict"`.
48
48
49
49
50
50
```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.
52
52
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.
Copy file name to clipboardExpand all lines: 1-js/2-first-steps/05-variables/article.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ Variables are used to store the information.
10
10
11
11
## A variable
12
12
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.
14
14
15
15
To create a variable in JavaScript, we need to use the `let` keyword.
16
16
@@ -72,12 +72,12 @@ let user = 'John',
72
72
message ='Hello';
73
73
```
74
74
75
-
...Or even in comma-first style:
75
+
...Or even in the "comma-first" style:
76
76
77
77
```js no-beautify
78
78
let user ='John'
79
-
,age =25
80
-
,message ='Hello';
79
+
,age =25
80
+
,message ='Hello';
81
81
```
82
82
83
83
Technically, all these variants do the same. So, it's a matter of personal taste and aestetics.
0 commit comments