Presentation is loading. Please wait.

Presentation is loading. Please wait.

Input & Output Functions JavaScript is special from other languages because it can accept input and produce output on the basis of that input in the same.

Similar presentations


Presentation on theme: "Input & Output Functions JavaScript is special from other languages because it can accept input and produce output on the basis of that input in the same."— Presentation transcript:

1

2 Input & Output Functions 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 refresh the page to see the output.

3 Functions to show output 1) window.alert() 2) window.status() 3) document.write() 4) document.writeln()

4 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 disappears. The message to be displayed has to be passed as parameter window.alert().

5 Code example window.alert ("Hello"); The above code displays a message box with the string "Hello" on it.

6 Output Functions 2) window.status() This function show the message in status- bar. The message to be displayed has to be passed as parameter window.status().

7 Code example window.status ="Welcome to JavaScript"; The above code displays a message "Welcome to JavaScript" in the status bar.

8 Output Functions 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(). 4) document.writeln() This function works as the same way as document.write().

9 Code example document.write( "Welcome to JavaScript"); document.close(); The above code displays a message "Welcome to JavaScript" on the page.

10 Code example You can also add HTML tags with document.write() document.writeln( " "); document.writeln( "Welcome to JavaScript"); document.write( " "); Here you can see HTML tag is added so the string "Welcome to JavaScript" is printed in bold format.

11 Functions: taking Input from User 1) window.prompt() 2) window.confirm()

12 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")

13 Code example var s; s = window.prompt("What is your name ?", "your name here" ); document.writeln( s); The above program asks user his name, and when the user enters his name and press "OK", the name of user is printed.

14 Code example 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

15 Input Functions 2) window.confirm() This function prompts 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.

16 Code example 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");


Download ppt "Input & Output Functions JavaScript is special from other languages because it can accept input and produce output on the basis of that input in the same."

Similar presentations


Ads by Google