BW Network
    Login | Register 

Stats
3 visitors online
Guests: 3
Registered: 0



SafeSurf Logo

Website BuildingTutorials → Javascript: Basic Implementation (Part 2)

The Basic Implementation

<html>
<head>
<title>BW: Javascript Tutorial!
</title>
<script language="javascript">
function alert()
{
name = prompt("What is your name","Name here");
document.write("Welcome " + name + ", to my website!");
}
</script>
</head>
<body onLoad="alert()">
</body>
</html>

Key points:

  • The Javascript goes in between the </title> & </head> tags.
  • The 'function alert();' text defines a function, called 'alert'.
  • 'name = prompt("What is your name","Name here");' defines a variable called 'name', which will be equal to the value the prompt returns.
  • 'document.write("Welcome " + name + ", to my website!");' writes some text and the 'name' variable to the page.
  • The <body onLoad="alert();"> tells the browser to execute the 'alert' function when the page has loaded.

Before continuing make sure you try out the code yourself and experiment with it!

External Scripts

External scripts allow you to create a .js file with the javascript in it and then only put one line of javascript coding in your html page - this makes it neater and is more efficient if one javascript is being used on more than one page.

test.html:
<html>
<head>
<title>Title
</title>
<script language="javascript" src="greeting.js">
</script>
</head>
<body>
</body>
</html>

greeting.js
age = prompt("How old are you?","21");
alert("You are " + age + " years old!);

'<script>' Abbreviation

You can also get away with shortening the opening Javascript tag, without any problems most the time. Instead of: <script language="javascript"></script> Use: <script></script>
The reason this works fine is because Javascript is the default scripting language for most browsers.

→ Strings & Variables →


  1. Introduction
  2. Part 1: Further Introduction
  3. Part 2: Basic Implementation
  4. Part 3: Strings & Variables
  5. Part 4: Forms
  6. Part 5: Login Script