HTML Scripts

HTML scripts usually refer to computer programs that are executed on the client side in the user's web browser.

From the previous lessons you already know that HTML is used to structure the web site, and CSS to stylize that structure (HTML Styles). Both of these technologies are adequate to make a static HTML page.  But, modern web sites are all about interactivities. Well, it is the client-side scripting language that adds the interactivities to the web pages. By client-side script, it means the script performs its action on the visitor’s browser without relying on the web server where the actual files are located.

HTML supports many client-side scripting languages, but, JavaScript is the most popular and widely used client-side scripting language.

Some uses of scripts (JavaScript)

Following is a small list of what you can do with JavaScript.

  • Google auto-suggest: while you type in the google search box, it shows you similar search terms with a drop down list.
    google auto suggest
  • Facebook social plug-ins: To embed facebook plug-ins ex. like box in your site JavaScript code is used.
    Facebook like box social plugins
  • Gmail: When you click Inbox. Trash, Spam etc inside gmail, the content loads without reloading the page which is done by JavaScript.
    gmail
  • Google analytics: To track website’s visitors’ number, traffic sources etc google analytics provide a JavaScript code to put on the website.
    google analytics
  • Image sliders: Lots of modern sites including the news sites use rotating image sliders in their first page.
    image slider
  • Web based instant messengers: You may have come across sites that interact with their visitors with instant messaging system added in the web site.
    web bases instant messenger
  • Form validation: When you submit a form without filling any required field, you might have seen any kind of alert which is a very widely used JavaScript action.
    form validation

How scripts (JavaScript) work with HTML document

When you run a web page in a browser, the browser takes the elements of the page and draws a map which is known as DOM (short for Document Object Model). Using DOM, browsers understand the structure of an HTML document and relationship among the HTML elements, attributes, and contents. JavaScript can read this DOM and perform its action in the web page.

Considering an HTML document as a tree and its elements as nodes of that tree, you can resemble it with DOM.

A simple HTML page and its DOM
The following is the code of a very simple HTML page-

<!doctype html>
<html lang="en">
    <head>
        <title>Example of external script</title>
    </head>
    <body>
        <p>This is a paragraph.</p>
        <a href="http://schoolsofweb.com/category/html-for-beginners/">Browse the table of contents.</a>
        <p>This is another paragraph.</p>
        <script src="default.js" type="text/javascript"></script>
    </body>
</html>

Here is the DOM representation of the above page-
DOM of a simple HTML page

Integrating scripts to HTML pages

There are two ways to include scripts in a HTML page-

  1. External script
  2. Internal script or  embedded script

You can add JavaScript code either in head element or in body element. But, to get better performance, it is advised to include it just before the closing </body> tag.

External script

To include an external script use script element and reference the external script by the src attribute. You can add this script element inside the head element or in the body element.

Example:
The HTML file

<!doctype html>
 <html lang="en">
     <head>
         <title>Example of external script</title>
     </head>
     <body>
         <p>This is a paragraph.</p>
         <a href=”http://schoolsofweb.com/category/html-for-beginners/”>Browse the table of contents.</a>
         <p>This is another paragraph.</p>
         <script src=”default.js” type=”text/javascript”></script>
     </body>
 </html>


default.js file
alert(“This is a JavaScript pop up.”);

Download Code334 downloads

Output:
If you run the above page, it will display the following pop up box with the message – This is a JavaScript pop up.

a javascript pop up

In the above example in HTML file, the JavaScript file default.js is added in line 11. You can load multiple scripts in one HTML document.

Pros:
It is easier to manage one external script than managing same script embedded in different HTML pages.

Internal script or embedded script

Here, JavaScript codes are written directly between the <script> and </script> tags in HTML document.

In this method no src attribute is used as there is no need to reference any external script.
Example:

<!doctype html>
<html lang="en">
    <head>
        <title>Example of embedded script</title>
    </head>
    <body>
        <p>This is a paragraph.</p>
        <a href=”http://schoolsofweb.com/category/html-for-beginners/">Browse the table of contents.</a>
        <p>This is another paragraph.</p>
        <script> alert(“This is a JavaScript pop up.”);</script>
    </body>
</html>
Download Code321 downloads

Output:
If you run the above page, it will display the same above pop up box that you see in the external script example with the message – This is a JavaScript pop up.

In the above example, JavaScript code is embedded directly in line 11.

Cons:
Embedded script is not a preferable way to add JavaScript in your HTML page. To add the same functionality in multiple pages, you have to add the same JavaScript code to every time.

How JavaScript code is triggered

In a browser there generate a lots of events against various occurrences happened in the browser. For example, when a page completed loading an event triggered. Other events could be when a key is pressed, or, when the cursor is placed over a link, or, when a button is pressed etc. You can write JavaScript to respond to any of the event when it occurs. This is how web pages become interactive.

When an event occurs, an event handler responds. This respond may include calling a JavaScript code. Event handlers are written as attributes of an HTML element. The following is a list of some event handlers-

onclick: The onclick event handler is triggered when visitor clicks on a element where the event handler is defined. Let’s see how this event handler works.
Example:

<!doctype html>
<html lang="en">
    <head>
        <script type=”text/javascript”>
            function showPopup(){
                alert(“This is a JavaScript pop-up.”);
            }
        </script>
    </head>
    <body>
        <a href="#" onclick=”showPopup();”>Click here and a JavaScript pop-up will appear.</a>
    </body>
</html>
Download Code308 downloads

In the above example when a visitor clicks on the link above, a popup window will appear. The onclick event handler is used in the a element which calls showPopup() JavaScript function when a visitor click on the click.

onload: This event handler is triggered when a web page where it is written is finished loading.

onchange: This event handler is triggered when content or value of an element is changed.

onmouseover: This event handler is triggered when mouse pointer over an element.

onkeypress: This event handler is triggered when a key is pressed while the cursor is on that specified element.

How to hide scripts from non-supporting browsers

Because of the prevalent use of scripting languages (ex. JavaScript), modern browsers support these. But, there may have a very small percentage of people still using old browsers that may not support scripts. Also, there is option in browsers to disable scripts. In both cases, the scripts written for the HTML page will be displayed on the browsers. To hide scripting codes from to display, write scripting code inside HTML comments. Browsers which can’t understand scripting codes will consider these codes as comments and will ignore but supporting browsers will recognize the codes and will act accordingly.

Example:

<!doctype html>
<html lang="en">
    <head>
        <title>Example of hiding scripting codes from unsupported browsers</title>
        <script type=”text/javascript”>
            <!--
            function showPopup(){
                alert(“This is a JavaScript pop-up.”);
            }
            -->
        </script>
    </head>
    <body>
        <a onclick=”showPopup();”>Click here and a JavaScript pop-up will appear.</a>
    </body>
</html>
Download Code286 downloads

In the example above, JavaScript codes (line 7 and 8) are written between HTML comments (line 6 and line 9). You’ll know more about comments in HTML Comments lesson.

noscript element: request visitors to activate script

It’s not enough to hide scripting codes from non-supporting browsers. Modern web user experience is deeply dependent on user interaction.  Visitors using unsupported browser may miss these interactions and also important information which rely on scripting codes.

To inform users that they are missing required interaction, you can use noscript element. Display inconvenient message inside this element. Script-unsupported browsers will display this content while supported browser will ignore this.

As the inconvenience message need  to show to visitors, place this noscript element inside the body element.

Example:

<!doctype html>
<html lang="en">
    <head>
        <title>Example of hiding scripting codes from unsupported browsers</title>
        <script type=”text/javascript”>
            <!--
            function showPopup(){
                alert(“This is a JavaScript pop-up.”);
            }
            -->
        </script>
    </head>
    <body>
        <a onclick=”showPopup();”>Click here and a JavaScript pop-up will appear.</a>
        <noscript>
            Currently JavaScript is disabled in your browser. Please turn on it.
        </noscript>
    </body>
</html>
Download Code258 downloads

If your browser doesn’t support Javascript, you’ll see the text written inside the noscript element otherwise you won’t see that message.

<< HTML Styles : Previous Lesson Next Lesson : HTML Body >>


Pin It

↑ Back to Top