As mentioned in the previous tutorial, javascript can also be written in an external file.
Lets see how this is done.

An external JavaScript is written in *.js file.

For our earlier example let us now, add the javascript to an external file “Hellos.js

Hellos.js

function callmethod()
{
document.write(“HELLO WORLD!!”);
}

Just modify the script tag as follows:

 

<script type=”text/Javascript” src=”Hellos.js”>

</script>

 

Having learnt how to add a external JavaScript file, lets look into some of the interaction options available.

1. Alert

We use an Alert when we want to display some information to the user, before proceeding with the data entered by the user.

Syntax  alert (“Your Message”);

Example

<html>
<head>
</head>

<body>

<input type =”button” value=”Click This Button” onclick=”alert(‘THIS IS AN ALERT WINDOW’)” />

</body>
</html>

2. Confirm

Sometimes you may want to cross check with the user to proceed further. We use confirm in such cases.

Imagine, you have asked your user to enter his Bank Account Details, now before you proceed, its a good practice to inform the user that the Bank processing would be done by another website.
Alert window displays “OK” and “CANCEL

In the event handler you can specify the appropriate action to be taken when the user clicks OK or CANCEL.

function confirmmethod()
{
var result=confirm(“We are passing your information to a different site, click OK to proceed”);

if(result)
{
document.write(“SUCCESS”);
}

else
return;
}

Complete code is given below.

<html>
<head>
<script type=”text/javascript”>

function confirmmethod()
{
var result=confirm(“We are passing your information to a different site, click OK to proceed”);

if(result)
{
document.write(“SUCCESS”);
}

else
return;
}

</script>
</head>

<body>

<input type =”button” value=”Click This Button” onclick=confirmmethod() />

</body>
</html>

3.Prompt

Prompt is used when we want the user to enter some additional information.

function promptmethod()
{
var result=prompt(“Please enter something in the textbox below !”,””)

alert(“Ok, This is what you typed.. “+result);

}

Complete code is given below.

<html>
<head>
<script type=”text/javascript”>

function promptmethod()
{
var result=prompt(“Please enter something in the textbox below !”,””)

if(result)

alert(“Ok, This is what you typed.. “+result);

else return;

}

</script>
</head>

<body>

<input type =”button” value=”Click This Button” onclick=promptmethod() />

</body>
</html>