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/02-first-steps/01-hello-world/article.md
+24-25Lines changed: 24 additions & 25 deletions
Original file line number
Diff line number
Diff line change
@@ -1,15 +1,15 @@
1
1
# Hello, world!
2
2
3
-
The tutorial that you're reading is about core JavaScript, which is platform-independent. Further on, you will learn Node.JS and other platforms that use it.
3
+
The tutorial that you're reading is about core JavaScript, which is platform-independent. Later on, you'll learn about Node.JS and other platforms that use it.
4
4
5
-
But, we need a working environment to run our scripts, and, just because this book is online, the browser is a good choice. We'll keep the amount of browser-specific commands (like `alert`) to a minimum so that you don't spend time on them if you plan to concentrate on another environment like Node.JS. On the other hand, browser details are explained in detail in the [next part](/ui) of the tutorial.
5
+
But we need a working environment to run our scripts and, since this book is online, the browser is a good choice. We'll keep the amount of browser-specific commands (like `alert`) to a minimum so that you don't spend time on them if you plan to concentrate on another environment (like Node.JS). We'll focus on JavaScript in the browser in the [next part](/ui) of the tutorial.
6
6
7
-
So first, let's see how to attach a script to a webpage. For server-side environments, you can just execute it with a command like `"node my.js"` for Node.JS.
7
+
So first, let's see how we attach a script to a webpage. For server-side environments (like Node.JS), you can execute the script with a command like `"node my.js"`.
8
8
9
9
10
10
## The "script" tag
11
11
12
-
JavaScript programs can be inserted in any part of an HTML document with the help of the `<script>` tag.
12
+
JavaScript programs can be inserted into any part of an HTML document with the help of the `<script>` tag.
13
13
14
14
For instance:
15
15
@@ -35,48 +35,47 @@ For instance:
35
35
```
36
36
37
37
```online
38
-
You can run the example by clicking on the "Play" button in its right-top corner.
38
+
You can run the example by clicking the "Play" button in the right-top corner of the box above.
39
39
```
40
40
41
-
The `<script>` tag contains JavaScript code which is automatically executed when the browser meets the tag.
41
+
The `<script>` tag contains JavaScript code which is automatically executed when the browser processes the tag.
42
42
43
43
44
-
## The modern markup
44
+
## Modern markup
45
45
46
-
The `<script>` tag has a few attributes that are rarely used nowadays, but we can find them in old code:
46
+
The `<script>` tag has a few attributes that are rarely used nowadays but can still be found in old code:
47
47
48
-
The `type` attribute: <code><script <u>type</u>=...></code>
48
+
The `type` attribute: <code><script <u>type</u>=...></code>
49
+
: The old HTML standard, HTML4, required a script to have a `type`. Usually it was `type="text/javascript"`. It's not required anymore. Also, the modern HTML standard, HTML5, totally changed the meaning of this attribute. Now, it can be used for JavaScript modules. But that's an advanced topic; we'll talk about modules in another part of the tutorial.
49
50
50
-
: The old standard HTML4 required a script to have a type. Usually it was `type="text/javascript"`. It's not required anymore. Also, the modern standard totally changed the meaning of this attribute. Now it can be used for Javascript modules. But that's an advanced topic; we'll talk about modules later in another part of the tutorial.
51
-
52
-
The `language` attribute: <code><script <u>language</u>=...></code>
53
-
: This attribute was meant to show the language of the script. This attribute no longer makes sense, because JavaScript is the default language. No need to use it.
51
+
The `language` attribute: <code><script <u>language</u>=...></code>
52
+
: This attribute was meant to show the language of the script. This attribute no longer makes sense because JavaScript is the default language. There is no need to use it.
54
53
55
54
Comments before and after scripts.
56
-
: In really ancient books and guides, one may find comments inside `<script>`, like this:
55
+
: In really ancient books and guides, you may find comments inside `<script>` tags, like this:
57
56
58
57
```html no-beautify
59
58
<script type="text/javascript"><!--
60
59
...
61
60
//--></script>
62
61
```
63
62
64
-
This trick isn't used in modern JavaScript. These comments were used to hide the JavaScript code from old browsers that didn't know about a `<script>` tag. Since browsers released in the last 15 years don't have this issue, this kind of comment can help you identify really old code.
63
+
This trick isn't used in modern JavaScript. These comments hid JavaScript code from old browsers that didn't know how to process the `<script>` tag. Since browsers released in the last 15 years don't have this issue, this kind of comment can help you identify really old code.
65
64
66
65
67
66
## External scripts
68
67
69
68
If we have a lot of JavaScript code, we can put it into a separate file.
70
69
71
-
The script file is attached to HTML with the `src` attribute:
70
+
Script files are attached to HTML with the `src` attribute:
72
71
73
72
```html
74
73
<scriptsrc="/path/to/script.js"></script>
75
74
```
76
75
77
-
Here `/path/to/script.js` is an absolute path to the file with the script (from the site root).
76
+
Here,`/path/to/script.js` is an absolute path to the script file (from the site root).
78
77
79
-
It is also possible to provide a path relative to the current page. For instance, `src="https://github.com/script.js"` would mean a file `"script.js"` in the current folder.
78
+
You can also provide a relative path from the current page. For instance, `src="https://github.com/script.js"` would mean a file `"script.js"` in the current folder.
80
79
81
80
We can give a full URL as well. For instance:
82
81
@@ -95,15 +94,15 @@ To attach several scripts, use multiple tags:
95
94
```smart
96
95
As a rule, only the simplest scripts are put into HTML. More complex ones reside in separate files.
97
96
98
-
The benefit of a separate file is that the browser will download it and then store it in its [cache](https://en.wikipedia.org/wiki/Web_cache).
97
+
The benefit of a separate file is that the browser will download it and store it in its [cache](https://en.wikipedia.org/wiki/Web_cache).
99
98
100
-
After this, other pages that want the same script will take it from the cache instead of downloading it. So the file is actually downloaded only once.
99
+
Other pages that reference the same script will take it from the cache instead of downloading it, so the file is actually downloaded only once.
101
100
102
-
That saves traffic and makes pages faster.
101
+
That reduces traffic and makes pages faster.
103
102
```
104
103
105
104
````warn header="If `src` is set, the script content is ignored."
106
-
A single `<script>` tag can't have both the `src` attribute and the code inside.
105
+
A single `<script>` tag can't have both the `src` attribute and code inside.
107
106
108
107
This won't work:
109
108
@@ -113,7 +112,7 @@ This won't work:
113
112
</script>
114
113
```
115
114
116
-
We must choose: either it's an external `<script src="https://github.com/%E2%80%A6">` or a regular `<script>` with code.
115
+
We must choose either an external `<script src="https://github.com/%E2%80%A6">` or a regular `<script>` with code.
117
116
118
117
The example above can be split into two scripts to work:
119
118
@@ -127,9 +126,9 @@ The example above can be split into two scripts to work:
127
126
128
127
## Summary
129
128
130
-
- We can use a `<script>` tag to add JavaScript code to the page.
129
+
- We can use a `<script>` tag to add JavaScript code to a page.
131
130
- The `type` and `language` attributes are not required.
132
131
- A script in an external file can be inserted with `<script src="path/to/script.js"></script>`.
133
132
134
133
135
-
There is much more to learn 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 the JavaScript language, so we shouldn't distract ourselves from it. We'll be using a browser as a way to run JavaScript, which is very convenient for online reading, but yet one of many.
134
+
There is much more to learn about browser scripts and their interaction with the webpage. But let's keep in mind that this part of the tutorial is devoted to the JavaScript language, so we shouldn't distract ourselves with browser-specific implementations of it. We'll be using the browser as a way to run JavaScript, which is very convenient for online reading, but only one of many.
0 commit comments