One reason JavaScript is so popular is that it’s relatively easy to add JavaScript to a web page. All you need to do, at a minimum, is include an HTML script element in the page, specify "text/javascript" for the type attribute, and add whatever JavaScript you want:
<script type="text/javascript">
...some JavaScript
</script>
Installation is not required, nor do you have to torturously work through any odd library path configurations. JavaScript works, straight out of the box and in most web browsers, including the big five: Firefox, Internet Explorer, Chrome, Opera, and Safari. All you need to do is add a scripting block, and you’re in business.
Traditionally, you add JavaScript blocks to the head element in the document (delimited by opening and closing head tags), but you also can include them in the body element— or even in both sections. However, adding script to the body is not usually considered a good technique, as it makes it more difficult to find the script when you’re modifying it at a later time. The exception to this rule is when performance is an issue, which we'll since in the upcoming articles on JavaScript.
Hello World!
Also traditionally, the first example when learning a new programming language is
known as “Hello, World”—a simple application that prints out “Hello, World!” to the
user interface, whatever it may be. In the case of JavaScript, the user interface is the
web page.
Traditionally, you add JavaScript blocks to the head element in the document (delimited by opening and closing head tags), but you also can include them in the body element— or even in both sections. However, adding script to the body is not usually considered a good technique, as it makes it more difficult to find the script when you’re modifying it at a later time. The exception to this rule is when performance is an issue, which we'll since in the upcoming articles on JavaScript.
Hello World!
Also traditionally, the first example when learning a new programming language is
known as “Hello, World”—a simple application that prints out “Hello, World!” to the
user interface, whatever it may be. In the case of JavaScript, the user interface is the
web page.
The example below shows a web page with a JavaScript block that, using only one
line of JavaScript, pops open a small window commonly called an alert box with the
words “Hello, World!”
line of JavaScript, pops open a small window commonly called an alert box with the
words “Hello, World!”
The smallest JavaScript application: “Hello, World!”
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Hello, World!</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
alert("Hello, World!");
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Hello, World!</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
alert("Hello, World!");
</script>
</head>
<body>
</body>
</html>
- Copy and paste the above code into your text editor save as helloworld.html. The saved file will now look like an internet document just click on it, it will opened in your web browser with the alert "Hello World"
0 comments:
Post a Comment