Web Design & Development

[Web Design and Development Topics] [Javascript List]

Javascript Introduction

What is Javascript?

Javascript is a scripting language that makes it easy for nonprogrammers to improve a Web page. It was developed by the Netscape Corporation and was originally called Livescript. It was released as Livescript in June of 1995, and after Sun's endorsement the same year was renamed to Javascript for brand marketing purposes (Sun created Java, which at the time was the hottest programming language on the market). Javascript includes a convenient syntax, flexible variable types, and easy access to browser features. Plus it can be run on a browser without being compiled; the source code can be placed directly on the HTML document or referenced in a separate file at the programmer's discretion. Java applets are compiled and stored on the server as byte codes, but Javascript programs are simple ASCII text files.

Fundamental data types used in Javascript:

Processing a script

The processing of a script depends on the type of script and where it is located:

<script> Tag

The script tag is used to imbed a Javascript within an HTML document.

DOCUMENT.WRITE SAMPLE:

<body>
This is the body of my page ...
<script type="text/javascript">
<!--//
document.write ("Here is my Javascript document.write command stuff.")
//-->
</script>
</body>

Note: The <!--// and /--> are used to hide the Javascript from older browsers or browsers with Javascript turned off.

Statements or functions that require a parameter such as document.write() use parenthesis to surround their parameters. If more than one parameter is required, they are separated by commas.

Note: Javascript keywords, function and object names, and variable names are case-sensitive.

LETS ADD AN AUTOMATIC WINDOWS ALERT MESSAGE:

<script type="text/javascript">
<!--//
document.write ("Here is my Javascript document.write command stuff.");
window.alert ("This is a Window Alert.");
//-->
</script>

Embedding a Javascript

The Javascript specification includes a SRC attribute to the <script> tag that enables you to embed a Javascript script in the page, similar to an image. To do this:

  1. Place the Javascript program in a file with a .js extension, such as sample.js
  2. Include the <script> tag to refer to it, such as <script src="program.js">
  3. Be sure the script and the Javascript file are in the same directory on the Web server

Sample of code in HTML referring out to other Javascript file:
<script src="../template.js" type="text/javascript"></script>

Sample HTML file:

<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="external_js.js" type="text/javascript"></script>
</head>

<body>
I am a demo page with external JS
</body>
</html>

Sample external JS file: (in this case it is called external_js.js)

document.write ("Here is my Javascript document.write command stuff.");
window.alert ("This is a Window Alert.");