JavaScript is special from other languages because it can accept input and produce output on the basis of that input in the same page. That mean, you don't have to go to other page or refreash the page to see the output.
Functions to show output
1)window.alert():
2)window.status()
3)document.write()
4)document.writeln()
Functions for taking Input from User
1)window.prompt()
2)window.confirm()
Details of the Output Functions
1)window.alert(): This function show the message in a small messagebox . The messagebox contains the string and an "OK"button on it. If the user presses the "OK" button the messagebox dissapears. The message to be displayed has to be passed as parameter window.alert() .
Code example :
<html>
<body>
<script language="Javascript">
window.alert ("Hello");
</script>
</body>
</html>The above code displays a message box with the string "Hello" on it.
2)window.status(): This function show the message in statusbar . The message to be displayed has to be passed as parameter window.status() .
Code example :
<html>
<body>
<script language="Javascript">
window.status ="Welcome to JavaScript";
</script >
</body>
</html>
The above code displays a message "Welcome to JavaScript" in the status bar .
3)document.write(): This function print a string in the current page. The message to be displayed has to be passed as parameter document.write() .
Code example :
<html>
<body>
<script language="Javascript">
document.write(
"Welcome to JavaScript");
document.close();
</script >
</body>
</html>
The above code displays a message "Welcome to JavaScript" on the page .
You can also add HTML tags with document.write()
<html>
<body>
<script language="Javascript">
document.writeln( "<h3>");
document.writeln( "Welcome to JavaScript");
document.write( "</h3>");
</script >
</body>
</html>
Here you can see HTML tag <h3> is added so the string "Welcome to JavaScript" is printed in bold format .
4)document.writeln(): This function works as the same way as document.write() .
Details of the Input Functions
1)window.prompt(): This function is used to get any value from the user. This function takes two optional parameters . Returns the value entered by the user in String format, however if user press "cancel" returns "Null"
String window.prompt("Question","default answer")
Code example :
<html>
<body>
<script language="Javascript">
var s;
s = window.prompt("What is your name ?", "your name here" );
document.writeln( s);
</script >
</body>
</html>
The above program asks user his name, and when the user enters his name and press "OK", the name of user is printed.
Another example
<html>
<body>
<script language="Javascript">
var no1,no2, ans;
no1 = window.prompt("Give the first number ?", "0" ); // get the first input
no2 = window.prompt("Give the second number ?", "0" ); // get the first input
ans = parseFloat(no1) + parseFloat(no2);//convert to float and add them
document.writeln( ans);// print the answer
</script >
</body>
</html>
The above example takes two numbers from the user, convert them to float using
parseFloat(), add them up and show the added result .
2)window.confirm() : This function promts the user to confirm the decision. It shows a messageBox with two buttons "OK" and "CANCEL" on it. If user press "OK" it returns "true" , if user press "CANCEL" it returns false.
<html>
<body>
<script language="Javascript">
var no1,no2, ans;
ans = window.confirm("Are you ready?")
if (ans==true) // check users response
document.writeln( "Start learning JavaScript");
else
document.writeln( "Try later");
</script >
</body>
</html>
So , that's it . Hope you have enjoyed the tutorial. Thank You.
Back to Tutorial Index page
Your comments are most welcome admin@koderguru.com
|