Embedding JavaScript

One of the great features of JavaScript is that you can pop it directly in your .html file using a few simple tags. While you can write your web page HTML code in notepad or any word processing application, notepad++ makes everything easier. It will  align your code, tell you when you’re doing something stupid or even if you are forgetting a tag or two. It’s magical, but you don’t need it to start coding in JavaScript.

So let’s get started. I’m going to assume that you already know what a generic HTML page looks like. By adding the <script> opening and closing tabs, as seen below, you can embed your code right in the file. When any new web browser attempts to set up your web page as you described it with HTML and CSS it will also start whatever task you set your JavaScript program to complete.

<html>
        <body>

                <script type=”text/javascript”>

                               document.write(“Hello World!”);
                 </script>

          </body>
</html>

Here, the tag <script type=”text/javascript”> tells the web browser that you are about to write some JavaScript and the </script> tag tells it to continue reading the HTML. The crazy looking statement in the middle is called a function. A function is basically a small program that takes some data (in this case, the data is “Hello World!”), and then does some task. In this case, the task is writing to the web page. The word “document” in this case refers to a collection, called a Class of functions, which are understood and executed by web browsers. One of the functions within the document Class is the write() function. The dot ( . ) operator allows us to use any function within that Class.

The code above will write “Hello World!” to a blank web page and display it for the user. You may be thinking, why not just do that in a paragraph tag? Well, we could have the statement appear only when an incorrect password was entered into a form or as a response when Hello was entered into a form. We’ll get into that later. For now, work on grasping the concept of a function. Can you think of any reason why forms in HTML can be named with a unique ID?

Advertisements

One Comment on “Embedding JavaScript”

  1. […] a function which takes a form as a parameter, gets the value typed into the form (using the form Class) and creates an alert which will pop up on the screen and print the message “Email: […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s