JavaScript Introduction
JavaScript is the most popular scripting language on the Internet. This language can be used in HTML and the web, and is more widely used on devices such as servers, PCs, laptops, tablets, and smartphones.
JavaScript Is a Scripting Language
JavaScript is a lightweight programming language.
JavaScript is programming code that can be inserted into HTML pages.
Once inserted into an HTML page, JavaScript can be executed by all modern browsers.
JavaScript is easy to learn.
What You Will Learn
Here is the main content you will learn in this tutorial.
JavaScript: Writing Directly to the HTML Output Stream
Example
document
.
write
(
"
This is a heading
"
)
;
document
.
write
(
"
This is a paragraph.
"
)
;
Try it »
![]() |
You can only use document.write in the HTML output. If you use it after the document has loaded, it will overwrite the whole document. |
|---|
JavaScript: Reacting to Events
Example
<button type="button" onclick="alert('Welcome!')">Click me!
button
Try it »
The alert() function is not commonly used in JavaScript, but it is very convenient for code testing.
The onclick event is just one of the many events you will learn about in this tutorial.
JavaScript: Changing HTML Content
Using JavaScript to manipulate HTML content is a very powerful feature.
Example
x
=
document
.
getElementById
(
"
demo
"
)
;
//
Find elements
x
.
innerHTML
=
"
Hello JavaScript
"
;
//
Change Content
Try it »
You will often see document.getElementById("some id"). This method is defined in the HTML DOM.
DOM (Document Object Model) (Document Object Model) is the official W3C standard for accessing HTML elements.
You will learn about the HTML DOM in several chapters of this tutorial.
JavaScript: Changing HTML Images
This example dynamically changes the HTML source (src):
Light Up the Bulb
<
script
function changeImage() { element=document.getElementById('myimage') if (element.src.match("bulbon")) { element.src="/images/pic_bulboff.gif"; } else { element.src="/images/pic_bulbon.gif"; } }
script
<img id="myimage" onclick="changeImage()" src="/images/pic_bulboff.gif" width="100" height="180">Click the bulb below to see the effect:
Click the bulb to turn this light on or off
Try it »
In the example above, the code element.src.match("bulbon") means: check
whether the value of the src attribute in the element contains bulbon this string; if the string exists, bulbonthe image src is updated to bulboff.gif; if there is no match for the bulbon string,src update it to bulbon.gif
JavaScript can change most attributes of any HTML element, not just images.
JavaScript: Changing HTML Styles
Changing the style of an HTML element is a variation of changing an HTML attribute.
Example
x
=
document
.
getElementById
(
"
demo
"
)
//
Find the element
x
.
style
.
color
=
"
#ff0000
"
;
//
Change the style
Try it »
JavaScript: Validating Input
JavaScript is often used to validate user input.
Example
if
isNaN
(
x
)
{
alert
(
"
Not a number
"
)
;
}
Try it »
The example above is only a basic validation. To use it in a production environment, stricter checks are needed; isNaN cannot detect spaces or consecutive spaces in the input. You can add a regular expression to check (explained in later chapters):
Example
if
(
isNaN
(
x
)
||
x
.
replace
(
/
(
^\
s
)
|
(
\
s
*$
)
/
g
,
"
"
)
==
"
"
)
{
alert
(
"
Not a number
"
)
;
}
Try it »
Did You Know?
|
| JavaScript and Java are two completely different languages, both conceptually and in design.
Java (invented by Sun) is a more complex programming language.
ECMA-262 是 JavaScript 标准的官方名称。
JavaScript 由 Brendan Eich 发明。它于 1995 年出现在 Netscape 中(该浏览器已停止更新),并于 1997 年被 ECMA(一个标准协会)采纳。 |
| --- | --- |
ECMAScript Versions
JavaScript has been standardized by ECMA (European Computer Manufacturers Association) through ECMAScript.
| Year | Name | Description |
|---|---|---|
| 1997 | ECMAScript 1 | The First Version |
| 1998 | ECMAScript 2 | Version Changes |
| 1999 | ECMAScript 3 | Added regular expressions |
| Added try/catch | ||
| ECMAScript 4 | Not released | |
| 2009 | ECMAScript 5 | Added "strict mode" |
| Added JSON support | ||
| 2011 | ECMAScript 5.1 | Version Changes |
| 2015 | ECMAScript 6 | Added classes and modules |
| 2016 | ECMAScript 7 | Added the exponentiation operator (**) |
| Added Array.prototype.includes |
ECMAScript 6 is also known as ECMAScript 2015.
ECMAScript 7 is also known as ECMAScript 2016.
whether the value of the src attribute in the element contains