Java Script (Methods and Functions)
Java Script (Methods and Functions)
JavaScript popup box are extremely useful when you start coding in JavaScript. They enable you to
write basic programs based on the Input/Process/Output very easily.
JavaScript provides three important Dialog Boxes, which include Alert Dialog Box for users,
Confirmation Dialog Box, and Prompt Dialog Box.
Alert Dialog Box is mainly used to display a notice, warning, or error to users. Basically, you cannot
customize dialog box icon or title, ... you can only provide the message that the dialog box will
display. In addition, Alert Dialog Box has only one OK button to close a dialog box.
To display a Alert Dialog Box, you call the alert(message) function, in which the message is the
content that the dialog box will display.
alert-example.js
<!DOCTYPE html>
<html>
<head>
<title>Alert Dialog Box</title>
<script type="text/javascript">
function testAlertDialog() {
Page 1
alert("Something Error!");
}
</script>
</head>
<body>
<h2>Alert Dialog Box</h2>
</body>
</html>
Confirmation Dialog Box is used to ask the user to confirm something. This dialog is very simple,
you cannot customize the icon or the title of the dialog box, you can only provide a message asking
the user to confirm. This dialog box has 2 OK and Cancel buttons.
To display a Confirmation Dialog Box you call the confirm(message) function, in which
the message is one requesting an user to confirm. If the user clicks the OK button, this function
returns true, otherwise if the user clicks the No button, this function returns false.
Page 2
confirm-example.js
<!DOCTYPE html>
<html>
<head>
<title>Confirmation Dialog Box</title>
<script type="text/javascript">
function testConfirmDialog() {
if(result) {
alert("OK Next lesson!");
} else {
alert("Bye!");
}
}
</script>
</head>
<body>
<h2>Confirmation Dialog Box</h2>
</body>
</html>
Page 3
3- Prompt Dialog Box
Prompt Dialog Box is used for users to enter an information. This dialog box is very simple. It
includes a Text Field for users to enter information. The dialog box has 2 OK and Cancel buttons.
To display a Prompt Dialog Box you call the prompt(message, defaultValue) function in which
the message is one for user. defaultValue is default value prefilled in the Text Field.
If an user clicks OK, the function returns contents on Text Field, otherwise, if the user
clicks Cancel, the function returns null.
prompt-example.js
<!DOCTYPE html>
<html>
<head>
<title>Prompt Dialog Box</title>
<script type="text/javascript">
function testPromptDialog() {
if(result != null) {
alert("Your age is " + result);
}
}
</script>
</head>
<body>
<h2>Prompt Dialog Box</h2>
Page 4
<button onclick="testPromptDialog()">Click me!</button>
</body>
</html>
Page 5
JavaScript Functions
JavaScript functions are used to perform operations. We can call JavaScript function many times to
reuse the code.
<!DOCTYPE html>
<html>
<head>
<title> ONE </title>
<script>
function msg()
{
alert("hello! this is message");
}
</script>
Page 6
</head>
<body>
<input type="button" onclick="msg()" value="call function"/>
</body>
</html>
We can call function by passing arguments. Let’s see the example of function that has one argument.
<!DOCTYPE html>
<html>
<head>
<title> ONE </title>
<script>
function getcube(number)
{
alert(number*number*number);
}
</script>
</head>
<body>
<form>
<input type="button" value="click" onclick="getcube(4)"/>
</form>
</body>
</html>
We can call function that returns a value and use it in our program. Let’s see the example of function
that returns value.
<!DOCTYPE html>
<html>
<head>
Page 7
<title> ONE </title>
<script>
function getInfo(){
return "Are you a student at Zetech University?";
}
document.write(getInfo());
</script>
</head>
<body>
</body>
</html>
JavaScript Math
The JavaScript math object provides several constants and methods to perform mathematical
operation. Unlike date object, it doesn't have constructors.
Methods Description
ceil() It returns a smallest integer value, greater than or equal to the given number.
Page 8
floor() It returns largest integer value, lower than or equal to the given number.
Page 9
JavaScript Events
The change in the state of an object is known as an Event. In html, there are various events which
represents that some activity is performed by the user or by the browser. When javascript code is
included in HTML, js react over these events and allow the execution. This process of reacting over
the events is called Event Handling. Thus, js handles the HTML events via Event Handlers.
For example, when a user clicks over the browser, add js code, which will execute the task to be
performed on the event.
Mouse events:
mouseover onmouseover When the cursor of the mouse comes over the element
mousedown onmousedown When the mouse button is pressed over the element
mouseup onmouseup When the mouse button is released over the element
Keyboard events:
Keydown & Keyup onkeydown & onkeyup When the user press and then release the key
Form events:
Page 10
change onchange When the user modifies or changes the value of a form element
Window/Document events
load onload When the browser finishes the loading of the page
unload onunload When the visitor leaves the current webpage, the browser unloads
it
resize onresize When the visitor resizes the window of the browser
<html>
<head>
<script language="Javascript" type="text/Javascript">
function clickevent()
{
document.write("ZETECH UNIVERSITY");
}
</script>
</head>
<body>
<form>
<input type="button" onclick="clickevent()" value="Where do yu study?"/>
</form>
</body>
</html>
Page 11
Mouse Over Event Example:
<html>
<head>
</head>
<script language="Javascript" type="text/Javascript">
function mouseoverevent()
{
alert("I am a student at Zetech University");
}
</script>
<body>
<h1> Javascript Events </h1>
<p onmouseover="mouseoverevent()"> Keep cursor over me</p>
</body>
</html>
<html>
<head></head>
<script>
function focusevent()
{
document.getElementById("input1").style.background=" aqua";
}
</script>
<body>
<h2> Enter something here</h2>
<input type="text" id="input1" onfocus="focusevent()"/>
</body>
</html>
Page 12
Key Down Event Example
<html>
<head></head>
<script>
function keydownevent()
{
document.getElementById("input1");
alert("Pressed a key");
}
</script>
<body>
<h2> Enter something here</h2>
<input type="text" id="input1" onkeydown="keydownevent()"/>
</body>
</html>
<html>
<head></head>
<script>
<!--
document.write("The page is loaded successfully");
//-->
</script>
</br>
<body onload="window.alert('Page successfully loaded');">
</body>
</html>
Page 13