Updated article.md for syntax / grammar
Sorry for the delay!
This commit is contained in:
parent
e62dfd5254
commit
52e2819264
1 changed files with 24 additions and 25 deletions
|
@ -1,15 +1,15 @@
|
||||||
# Hello, world!
|
# Hello, world!
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
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.
|
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"`.
|
||||||
|
|
||||||
|
|
||||||
## The "script" tag
|
## The "script" tag
|
||||||
|
|
||||||
JavaScript programs can be inserted in any part of an HTML document with the help of the `<script>` tag.
|
JavaScript programs can be inserted into any part of an HTML document with the help of the `<script>` tag.
|
||||||
|
|
||||||
For instance:
|
For instance:
|
||||||
|
|
||||||
|
@ -35,25 +35,24 @@ For instance:
|
||||||
```
|
```
|
||||||
|
|
||||||
```online
|
```online
|
||||||
You can run the example by clicking on the "Play" button in its right-top corner.
|
You can run the example by clicking the "Play" button in the right-top corner of the box above.
|
||||||
```
|
```
|
||||||
|
|
||||||
The `<script>` tag contains JavaScript code which is automatically executed when the browser meets the tag.
|
The `<script>` tag contains JavaScript code which is automatically executed when the browser processes the tag.
|
||||||
|
|
||||||
|
|
||||||
## The modern markup
|
## Modern markup
|
||||||
|
|
||||||
The `<script>` tag has a few attributes that are rarely used nowadays, but we can find them in old code:
|
The `<script>` tag has a few attributes that are rarely used nowadays but can still be found in old code:
|
||||||
|
|
||||||
The `type` attribute: <code><script <u>type</u>=...></code>
|
The `type` attribute: <code><script <u>type</u>=...></code>
|
||||||
|
: 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.
|
||||||
|
|
||||||
: 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.
|
The `language` attribute: <code><script <u>language</u>=...></code>
|
||||||
|
: 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.
|
||||||
The `language` attribute: <code><script <u>language</u>=...></code>
|
|
||||||
: 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.
|
|
||||||
|
|
||||||
Comments before and after scripts.
|
Comments before and after scripts.
|
||||||
: In really ancient books and guides, one may find comments inside `<script>`, like this:
|
: In really ancient books and guides, you may find comments inside `<script>` tags, like this:
|
||||||
|
|
||||||
```html no-beautify
|
```html no-beautify
|
||||||
<script type="text/javascript"><!--
|
<script type="text/javascript"><!--
|
||||||
|
@ -61,22 +60,22 @@ Comments before and after scripts.
|
||||||
//--></script>
|
//--></script>
|
||||||
```
|
```
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
|
|
||||||
## External scripts
|
## External scripts
|
||||||
|
|
||||||
If we have a lot of JavaScript code, we can put it into a separate file.
|
If we have a lot of JavaScript code, we can put it into a separate file.
|
||||||
|
|
||||||
The script file is attached to HTML with the `src` attribute:
|
Script files are attached to HTML with the `src` attribute:
|
||||||
|
|
||||||
```html
|
```html
|
||||||
<script src="/path/to/script.js"></script>
|
<script src="/path/to/script.js"></script>
|
||||||
```
|
```
|
||||||
|
|
||||||
Here `/path/to/script.js` is an absolute path to the file with the script (from the site root).
|
Here, `/path/to/script.js` is an absolute path to the script file (from the site root).
|
||||||
|
|
||||||
It is also possible to provide a path relative to the current page. For instance, `src="script.js"` would mean a file `"script.js"` in the current folder.
|
You can also provide a relative path from the current page. For instance, `src="script.js"` would mean a file `"script.js"` in the current folder.
|
||||||
|
|
||||||
We can give a full URL as well. For instance:
|
We can give a full URL as well. For instance:
|
||||||
|
|
||||||
|
@ -95,15 +94,15 @@ To attach several scripts, use multiple tags:
|
||||||
```smart
|
```smart
|
||||||
As a rule, only the simplest scripts are put into HTML. More complex ones reside in separate files.
|
As a rule, only the simplest scripts are put into HTML. More complex ones reside in separate files.
|
||||||
|
|
||||||
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).
|
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).
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
That saves traffic and makes pages faster.
|
That reduces traffic and makes pages faster.
|
||||||
```
|
```
|
||||||
|
|
||||||
````warn header="If `src` is set, the script content is ignored."
|
````warn header="If `src` is set, the script content is ignored."
|
||||||
A single `<script>` tag can't have both the `src` attribute and the code inside.
|
A single `<script>` tag can't have both the `src` attribute and code inside.
|
||||||
|
|
||||||
This won't work:
|
This won't work:
|
||||||
|
|
||||||
|
@ -113,7 +112,7 @@ This won't work:
|
||||||
</script>
|
</script>
|
||||||
```
|
```
|
||||||
|
|
||||||
We must choose: either it's an external `<script src="…">` or a regular `<script>` with code.
|
We must choose either an external `<script src="…">` or a regular `<script>` with code.
|
||||||
|
|
||||||
The example above can be split into two scripts to work:
|
The example above can be split into two scripts to work:
|
||||||
|
|
||||||
|
@ -127,9 +126,9 @@ The example above can be split into two scripts to work:
|
||||||
|
|
||||||
## Summary
|
## Summary
|
||||||
|
|
||||||
- We can use a `<script>` tag to add JavaScript code to the page.
|
- We can use a `<script>` tag to add JavaScript code to a page.
|
||||||
- The `type` and `language` attributes are not required.
|
- The `type` and `language` attributes are not required.
|
||||||
- A script in an external file can be inserted with `<script src="path/to/script.js"></script>`.
|
- A script in an external file can be inserted with `<script src="path/to/script.js"></script>`.
|
||||||
|
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue