Try it here
Subscribe
JavaScript hello world

Writing Your First JavaScript Program

writing_your_first_javascript_program

How to Add JavaScript to a Page

The web browser is usually expecting HTML, so you must specifically tell the browser when JavaScript is coming by using the <script> tag.

The <script> tag is regular HTML.When the web browser encounters the closing </script> tag, it knows it's reached the end of the JavaScript program and can get back to its normal duties.

Much of the time, you'll add the <script> tag in the web page's <head> section, like this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/ html4/strict.dtd">
<html>
<head>
<title>My Web Page</title>
<script type="text/javascript">
</script>
</head>
<body>
</body>
</html>

The <script> tag's type attribute indicates the format and the type of script that follows. In this case, type="text/javascript" means the script is regular text (just like HTML) and that it's written in JavaScript.

If you're using HTML5, life is even simpler. You can skip the type attribute entirely:

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>My Web Page</title>
<script>
</script>
</head>
<body>
</body>
</html>

You then add your JavaScript code between the opening and closing <script> tags:

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>My Web Page</title>
<script>
 alert('hello world!');
</script>
</head>
<body>
</body>
</html>

In many cases, you'll put the <script> tags in the page's <head> in order to keep your JavaScript code neatly organized in one area of the web page.

However, it's perfectly valid to put <script> tags anywhere inside the page's HTML.

In fact, it's common to put <script> tags just below the closing </body> tag?this approach makes sure the page is loaded and the visitor sees it before running any JavaScript.

External JavaScript Files

Using the <script> tag as discussed in the previous section lets you add JavaScript to a single web page. But many times you'll create scripts that you want to share with all of the pages on your site.

An external JavaScript file is a text file containing JavaScript code and ending with the file extension .js - test.js, for example. The file is linked to a web page using the <script> tag. For example, to add this JavaScript file to your home page, you might write the following:

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>My Web Page</title>
<script  src="test.js" >
</script>
</head>
<body>
</body>
</html>

The src attribute of the <script> tag works just like the src attribute of an <img> tag, or an <a> tag's href attribute.

Your First JavaScript Program

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>My Web Page</title>
<script>
alert("Writing Hello World Using document.write...");
document.write('<p>Hello world!</p>');
</script>
</head>
<body>
</body>
</html>

See also JavaScript Functions

Writer profile pic

Admin on Apr 21, 2020 at 12:11 am


If you like dEexams.com and would like to contribute, you can write your article here or mail your article to admin@deexams.com . See your article appearing on the dEexams.com main page and help others to learn.



Post Comment

Comments( 0)

WEB TECHNOLOGY

Articles

×

Forgot Password

Please enter your email address below and we will send you information to change your password.