If you have enough time and desire to start from scratch, read on 🙂
<!--Meta tag for specifying encoding
<p>Start document …
alert( 'Hello World!
<p>…End of document
This example uses the following elements:
<script> ... </script>
scriptcontains executable code. Previous HTML standards required a mandatory attribute
type, but now it is no longer needed. Simple enough
Browser when sees
- Starts displaying the page, shows part of the document until
- Upon encountering a tag
- When completed, returns to HTML mode and only then displays the rest of the document.Try this example in action and see for yourself.
)Displays a message box and waits until the visitor clicks OK.
Encoding and tag
When you try to make the same file on your disk and run it, you may encounter a problem – “krakozyably”, “squares” and “questions” are displayed instead of the Russian text.
To make everything good, you need:
- Make sure that
HEADthere is a string
<meta charset="utf-8">. If you open the file from the disk, then it will tell the browser the encoding.
- Make sure that the editor saved the file in UTF-8 encoding, and not, say, in
Specifying the encoding is part of normal HTML, I mention this “just in case” so that there are no surprises when running examples locally.
Modern markup for SCRIPT
In old scripts, the design of the tag
SCRIPTwas a bit more complicated. In the outdated manuals you can find the following elements:Attribute
<script type=…>Unlike HTML5, the HTML 4 standard required the mandatory indication of this attribute. It looked like this:
type, the script will not be executed.
In modern development attribute is
In very old manuals and books, it is sometimes recommended to use HTML comments inside
It looks like this:
The browser for which such tricks were intended, a very old Netscape, has long died. Therefore, there is no need for these comments.
So, to insert the script, we simply write
<script>, without additional attributes and comments.
Create it on disk, open it in the browser, make sure everything works.