Bits & Bytes

A Simple JavaScript Code Example

To begin, create an HTML file and copy the code from our post A Basic HTML Page Template, and we will paste our JavaScript into it. You can use a simple text editor, like Notepad, to create the file. I will use “SimpleJavascriptExample.html” as the name of my file and paste this code inside the body tags:

<script type="text/javascript">
  document.write("Welcome to XoaX.net!");
</script>

After pasting the JavaScript code above into the HTML file, the code in the file should look like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <title>XoaX.net</title>
</head>
<body>

<script type="text/javascript">
  document.write("Welcome to XoaX.net!");
</script>

</body>
</html>

Now, if we open the file with a browser, we will see the message “Welcome to XoaX.net!” printed out. This exact message could be written without JavaScript by just putting the message inside the body tags by itself. However, we are preparing the way to write more complex JavaScript examples.

In this example, we have one line of JavaScript code inside of the script tags. The script tags, along with the type designation “text/javascript,” tell the browser that the enclosed code is JavaScript. The single line of JavaScript code inside the tags tells the browser to call the write() function on the document object with the argument “Welcome to XoaX.net!”. The semicolon at the end of the line signals the end of the command.

Tags: , , , , , , , ,

By: Michael Hall

2 Responses to “A Simple JavaScript Code Example”

  1. Romain says:

    Heck of a job there, it absluotely helps me out.

  2. […] with the code from our earlier JavaScript post, we have a simple HTML file, which I have called “JavaScriptBasis.html” in this post. […]

Leave a Reply

*