HTML and Javascript Tutorial

HTML Basic Document

 

 

 

 

 

Visible text goes here

 

 

Heading Elements

 

Largest Heading

 

 

. . .

 

 

. . .

 

 

. . .

 

 

. . .

 

 

Smallest Heading

 

Text Elements

 

This is a paragraph

 

(line break)

 


(horizontal rule) 

 

This text is preformatted

 

Logical Styles

This text is emphasized

This text is strong

This is some computer code

Physical Styles

This text is bold

This text is italic

Links, Anchors, and Image Elements

This is a Link

Alternate Text

Send e-mail

A named anchor:

Useful Tips Section

Jump to the Useful Tips Section

Unordered list

 

 

 

    • First item

 

 

    • Next item

 

 

 

Ordered list

 

 

 

    1. First item

 

 

    1. Next item

 

 

 

Definition list

 

 

 

First term

 

 

Definition

 

 

Next term

 

 

Definition

 

 

 

Tables

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

someheader someheader
sometext sometext

 

Frames

 

 

 

 

Forms

 

 

 

 

 

Entities

< is the same as <

> is the same as >

© is the same as ©

Other Elements

 

 

 

Text quoted from some source.

 

 

 

 

Address 1

Address 2

City

 

 

Commonly Used Character Entities

Note Entity names are case sensitive!

Result

Description

Entity Name

Entity Number

non-breaking space    
< less than < <
> greater than > >
& ampersand & &
¢ cent ¢ ¢
£ pound £ £
¥ yen ¥ ¥
euro
§ section § §
© copyright © ©
® registered trademark ® ®

The Meta Element

As we explained in the previous chapter, the head element contains general information (meta-information) about a document.

HTML also includes a meta element that goes inside the head element. The purpose of the meta element is to provide meta-information about the document.

Most often the meta element is used to provide information that is relevant to browsers or search engines like describing the content of your document.

Keywords for Search Engines

Some search engines on the WWW will use the name and content attributes of the meta tag to index your pages.

This meta element defines a description of your page:

 

This meta element defines keywords for your page:

 

The intention of the name and content attributes is to describe the content of a page.

However, since too many webmasters have used meta tags for spamming, like repeating keywords to give pages a higher ranking, some search engines have stopped using them entirely.

Uniform Resource Locators

Something called a Uniform Resource Locator (URL) is used to address a document (or other data) on the World Wide Web. A full Web address like this: http://www.w3schools.com/html/lastpage.htm follows these syntax rules:

scheme://host.domain:port/path/filename

The scheme is defining the type of Internet service. The most common type is http.

The domain is defining the Internet domain name like w3schools.com.

The host is defining the domain host. If omitted, the default host for http is www.

The :port is defining the port number at the host. The port number is normally omitted. The default port number for http is 80.

The path is defining a path (a sub directory) at the server. If the path is omitted, the resource (the document) must be located at the root directory of the Web site.

The filename is defining the name of a document. The default filename might be default.asp, or index.html or something else depending on the settings of the Web server.

URL Schemes

Some examples of the most common schemes can be found below:

Schemes

Access

file a file on your local PC
ftp a file on an FTP server
http a file on a World Wide Web Server
gopher a file on a Gopher server
news a Usenet newsgroup
telnet a Telnet connection
WAIS a file on a WAIS server

Accessing a Newsgroup

The following HTML code:

HTML Newsgroup

creates a link to a newsgroup like this HTML Newsgroup

Downloading with FTP

The following HTML code:

Download WinZip

creates a link to download a file like this: Download WinZip.

(The link doesn’t work. Don’t try it. It is just an example. W3Schools doesn’t really have an ftp directory.)

Link to your Mail system

The following HTML code:

someone@w3schools.com

creates a link to your own mail system like this:

Insert a Script into HTML Page

A script in HTML is defined with the

 

 

How to Handle Older Browsers

A browser that does not recognize the

VBScript:

 

New to HTML 4.0 is the ability to let HTML events trigger actions in the browser, like starting a JavaScript when a user clicks on an HTML element. Below is a list of attributes that can be inserted into HTML tags to define event actions.

Window Events

Only valid in body and frameset elements.

Attribute

Value

Description

onload script Script to be run when a document loads
onunload script Script to be run when a document unloads

Only valid in form elements.

Attribute

Value

Description

onchange script Script to be run when the element changes
onsubmit script Script to be run when the form is submitted
onreset script Script to be run when the form is reset
onselect script Script to be run when the element is selected
onblur script Script to be run when the element loses focus
onfocus script Script to be run when the element gets focus

Keyboard Events

Not valid in base, bdo, br, frame, frameset, head, html, iframe, meta, param, script, style, and title elements.

Attribute

Value

Description

onkeydown script What to do when key is pressed
onkeypress script What to do when key is pressed and released
onkeyup script What to do when key is released

Mouse Events

Not valid in base, bdo, br, frame, frameset, head, html, iframe, meta, param, script, style, title elements.

Attribute

Value

Description

onclick script What to do on a mouse click
ondblclick script What to do on a mouse double-click
onmousedown script What to do when mouse button is pressed
onmousemove script What to do when mouse pointer moves
onmouseout script What to do when mouse pointer moves out of an element
onmouseover script What to do when mouse pointer moves over an element
onmouseup script What to do when mouse button is released

Your Windows PC as a Web Server

If you want other people to view your pages, you must publish them.

To publish your work, you must save your pages on a web server.

Your own PC can act as a web server if you install IIS or PWS.

IIS or PWS turns your computer into a web server.

Microsoft IIS and PWS are free web server components.

IIS – Internet Information Server

IIS is for Windows system like Windows 2000, XP, and Vista. It is also available for Windows NT.

IIS is easy to install and ideal for developing and testing web applications.

IIS includes Active Server Pages (ASP), a server-side scripting standard that can be used to create dynamic and interactive web applications.

PWS – Personal Web Server

PWS is for older Windows system like Windows 95, 98, and NT.

PWS is easy to install and can be used for developing and testing web applications including ASP.

We don’t recommend running PWS for anything else than training. It is outdated and have security issues.

Windows Web Server Versions

Windows Vista Professional comes with IIS 6.

Windows Vista Home Edition does not support PWS or IIS.

Windows XP Professional comes with IIS 5.

Windows XP Home Edition does not support IIS or PWS.

Windows 2000 Professional comes with IIS 4.

Windows NT Professional comes with IIS 3 and also supports IIS 4.

Windows NT Workstation supports PWS and IIS 3.

Windows ME does not support PWS or IIS.

Windows 98 comes with PWS.

Windows 95 supports PWS.

HTML Summary

This tutorial has taught you how to use HTML to create your own web site.

HTML is the universal markup language for the Web. HTML lets you format text, add graphics, create links, input forms, frames and tables, etc., and save it all in a text file that any browser can read and display.

The key to HTML is the tags, which indicates what content is coming up.

XHTML

XHTML reformulates HTML 4.01 in XML.

CSS

CSS is used to control the style and layout of multiple Web pages all at once.

With CSS, all formatting can be removed from the HTML document and stored in a separate file.

CSS gives you total control of the layout, without messing up the document content.

JavaScript Tutorial

What is JavaScript?

JavaScript was designed to add interactivity to HTML pages

JavaScript is a scripting language

A scripting language is a lightweight programming language

JavaScript is usually embedded directly into HTML pages

JavaScript is an interpreted language (means that scripts execute without preliminary compilation)

Everyone can use JavaScript without purchasing a license

What can a JavaScript Do?

JavaScript gives HTML designers a programming tool – HTML authors are normally not programmers, but JavaScript is a scripting language with a very simple syntax! Almost anyone can put small “snippets” of code into their HTML pages

JavaScript can put dynamic text into an HTML page – A JavaScript statement like this: document.write(”

” + name + “

“) can write a variable text into an HTML page 

JavaScript can react to events – A JavaScript can be set to execute when something happens, like when a page has finished loading or when a user clicks on an HTML element

JavaScript can read and write HTML elements – A JavaScript can read and change the content of an HTML element

JavaScript can be used to validate data – A JavaScript can be used to validate form data before it is submitted to a server. This saves the server from extra processing

JavaScript can be used to detect the visitor’s browser – A JavaScript can be used to detect the visitor’s browser, and – depending on the browser – load another page specifically designed for that browser

JavaScript can be used to create cookies – A JavaScript can be used to store and retrieve information on the visitor’s computer

How to Put a JavaScript Into an HTML Page

 

 

 

 

 

Where to Put the JavaScript

JavaScripts in a page will be executed immediately while the page loads into the browser. This is not always what we want. Sometimes we want to execute a script when a page loads, other times when a user triggers an event.

Scripts in the head section: Scripts to be executed when they are called, or when an event is triggered, go in the head section. When you place a script in the head section, you will ensure that the script is loaded before anyone uses it.

 

 

 

 

Scripts in the body section: Scripts to be executed when the page loads go in the body section. When you place a script in the body section it generates the content of the page.

 

 

 

 

 

 

Scripts in both the body and the head section: You can place an unlimited number of scripts in your document, so you can have scripts in both the body and the head section.

 

 

 

 

 

 

 

Using an External JavaScript

Sometimes you might want to run the same JavaScript on several pages, without having to write the same script on every page.

To simplify this, you can write a JavaScript in an external file. Save the external JavaScript file with a .js file extension.

Note: The external script cannot contain the

 

 

 

 

 

JavaScript is Case Sensitive

Unlike HTML, JavaScript is case sensitive – therefore watch your capitalization closely when you write JavaScript statements, create or call variables, objects and functions.

JavaScript Statements

A JavaScript statement is a command to the browser. The purpose of the command is to tell the browser what to do.

This JavaScript statement tells the browser to write “Hello Dolly” to the web page:

document.write(“Hello Dolly”);

It is normal to add a semicolon at the end of each executable statement. Most people think this is a good programming practice, and most often you will see this in JavaScript examples on the web.

The semicolon is optional (according to the JavaScript standard), and the browser is supposed to interpret the end of the line as the end of the statement. Because of this you will often see examples without the semicolon at the end.

Note: Using semicolons makes it possible to write multiple statements on one line.

JavaScript Code

JavaScript code (or just JavaScript) is a sequence of JavaScript statements.

Each statement is executed by the browser in the sequence they are written.

This example will write a header and two paragraphs to a web page:

 

JavaScript comments can be used to make the code more readable.

JavaScript Comments

Comments can be added to explain the JavaScript, or to make it more readable.

Single line comments start with //.

This example uses single line comments to explain the code:

 

Using Comments to Prevent Execution

In this example the comment is used to prevent the execution of a single code line:

 

In this example the comments is used to prevent the execution of multiple code lines:

 

Using Comments at the End of a Line

In this example the comment is placed at the end of a line:

 

Variables are “containers” for storing information.

Declaring (Creating) JavaScript Variables

Creating variables in JavaScript is most often referred to as “declaring” variables.

You can declare JavaScript variables with the var statement:

var x;

var carname;

After the declaration shown above, the variables are empty (they have no values yet).

However, you can also assign values to the variables when you declare them:

var x=5;

var carname=”Volvo”;

After the execution of the statements above, the variable x will hold the value 5, and carname will hold the value Volvo.

Note: When you assign a text value to a variable, use quotes around the value.

Assigning Values to Undeclared JavaScript Variables

If you assign values to variables that have not yet been declared, the variables will automatically be declared.

These statements:

x=5;

carname=”Volvo”;

have the same effect as:

var x=5;

var carname=”Volvo”;

Redeclaring JavaScript Variables

If you redeclare a JavaScript variable, it will not lose its original value.

var x=5;

var x;

After the execution of the statements above, the variable x will still have the value of 5. The value of x is not reset (or cleared) when you redeclare it.

Conditional Statements

Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this.

In JavaScript we have the following conditional statements:

if statement – use this statement if you want to execute some code only if a specified condition is true

if…else statement – use this statement if you want to execute some code if the condition is true and another code if the condition is false

if…else if….else statement – use this statement if you want to select one of many blocks of code to be executed

switch statement – use this statement if you want to select one of many blocks of code to be executed

script type=”text/javascript”>

//If the time is less than 10,

//you will get a “Good morning” greeting.

//Otherwise you will get a “Good day” greeting.

var d = new Date();

var time = d.getHours();

if (time < 10)

{

document.write(“Good morning!”);

}

else

{

document.write(“Good day!”);

}

 

The JavaScript Switch Statement

You should use the switch statement if you want to select one of many blocks of code to be executed.

Syntax

switch(n)

{

case 1:

execute code block 1

break;

case 2:

execute code block 2

break;

default:

code to be executed if n is

different from case 1 and 2

}

 

JavaScript Popup Boxes

Alert Box

An alert box is often used if you want to make sure information comes through to the user.

When an alert box pops up, the user will have to click “OK” to proceed.

Syntax:

alert(“sometext”);

Confirm Box

A confirm box is often used if you want the user to verify or accept something.

When a confirm box pops up, the user will have to click either “OK” or “Cancel” to proceed.

If the user clicks “OK”, the box returns true. If the user clicks “Cancel”, the box returns false.

Syntax:

confirm(“sometext”);

Prompt Box

A prompt box is often used if you want the user to input a value before entering a page.

When a prompt box pops up, the user will have to click either “OK” or “Cancel” to proceed after entering an input value.

If the user clicks “OK” the box returns the input value. If the user clicks “Cancel” the box returns null.

Syntax:

prompt(“sometext”,”defaultvalue”);

JavaScript Functions

JavaScript Functions

To keep the browser from executing a script when the page loads, you can put your script into a function.

A function contains code that will be executed by an event or by a call to that function.

You may call a function from anywhere within the page (or even from other pages if the function is embedded in an external .js file).

Functions can be defined both in the and in the section of a document. However, to assure that the function is read/loaded by the browser before it is called, it could be wise to put it in the section.

How to Define a Function

The syntax for creating a function is:

Cite This Work

To export a reference to this article please select a referencing stye below:

Related Services

View all 

Female student working on a laptop

Dissertation Writing Service
From £136
Male student reading book

Dissertation Proposal Writing Service
From £124
Professor

You must be logged in to post a comment