Q1 alert and prompt are the methods for user interaction in
Q1. “alert()” and “prompt()” are the methods for user interaction in Java Script. Differentiate between them, give an example declaration/usage, describe their limitations and which popular browsers from the following support them. The popular browsers could be: Mozilla Firefox, Internet Explorer, MS Edge, Google Chrome, Safari and Opera. [1 Mark]
Q2. Differentiate between local and global variables in JavaScript and give a suitable example. [1 Mark]
Q3. Events on a web page can be of different types. Give examples of different events and how JavaScript handles them. Also give an example code of one of the events.
[2Marks]
Solution
An alert dialog box is mostly used to give a warning message to the users. For example, if one input field requires to enter some text but the user does not provide any input, then as a part of validation, you can use an alert box to give a warning message.
Ex:-
<html>
<head>
<script type=\"text/javascript\">
<!--
function getConfirmation(){
var retVal = confirm(\"Do you want to continue ?\");
if( retVal == true ){
document.write (\"User wants to continue!\");
return true;
}
else{
document.write (\"User does not want to continue!\");
return false;
}
}
//-->
</script>
</head>
<body>
<p>Click the following button to see the result: </p>
<form>
<input type=\"button\" value=\"Click Me\" onclick=\"getConfirmation();\" />
</form>
</body>
</html>
The prompt dialog box is very useful when you want to pop-up a text box to get user input. Thus, it enables you to interact with the user. The user needs to fill in the field and then click OK.
<html>
<head>
<script type=\"text/javascript\">
<!--
function getValue(){
var retVal = prompt(\"Enter your name : \", \"your name here\");
document.write(\"You have entered : \" + retVal);
}
//-->
</script>
</head>
<body>
<p>Click the following button to see the result: </p>
<form>
<input type=\"button\" value=\"Click Me\" onclick=\"getValue();\" />
</form>
</body>
</html>
Global variables are declared outside any function, and they can be accessed (used) on any function in the program. Local variables are declared inside a function, and can be used only inside that function. It is possible to have local variables with the same name in different functions. Even the name is the same, they are not the same. It\'s like two people with the same name. Even the name is the same, the persons are not.

