alert and prompt are the methods for user interaction in Jav

“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.

Solution

alert() is supported by all the browsers mentioned above.

The alert() method displays an alert box with a message on it and an OK button. It is commonly used to check if the user wants to submit the information. It prevents us to perform any other activities on page till we click ok or close the alert.

example:

<!DOCTYPE html>
<html>
<body>

<p>Example of alert box.</p>

<button onclick=\"myAlert()\">Submit</button>

<script>
function myAlert() {
alert(\"Hello! Do you want to submit!\");
}
</script>

</body>
</html>

prompt():

The function of prompt() method is to present a dialog box that prompts the user for input.

When a prompt box pops up, the user has click either \"OK\" or \"Cancel\" to proceed for submission of form.
It also prevents the user from accessing other parts of the page until the box is closed.

The prompt() method returns the value entered by the user if the user clicks \"OK\".
If the user clicks \"cancel\" the method returns null and it stays on the same page.

example:

<!DOCTYPE html>
<html>
<body>

<p>Prompt demo.</p>

<button onclick=\"myPrompt()\">Submit</button>

<p id=\"demo\"></p>

<script>
function myPrompt() {
var name = prompt(\"Please enter your name\");
if (name != null) {
document.getElementById(\"demo\").innerHTML =
\"Hello \" + name +\".\";
}
}
</script>

</body>
</html>

This is also supported by all the mentioned browser.

“alert()” and “prompt()” are the methods for user interaction in Java Script. Differentiate between them, give an example declaration/usage, describe their limi
“alert()” and “prompt()” are the methods for user interaction in Java Script. Differentiate between them, give an example declaration/usage, describe their limi

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site