Alarts, Prompts,Confirms, method-


JavaScript Dialog Boxes

JavaScript में user से interaction करने के लिए कुछ built-in dialog box methods उपलब्ध होते हैं। ये methods browser window पर popup box प्रदर्शित करते हैं।

मुख्य Dialog Box Methods निम्न हैं –

  1. alert()
  2. prompt()
  3. confirm()

1. alert() Method

alert() method web page पर एक alert dialog box प्रदर्शित करती है। इसे popup box भी कहा जाता है।

इस dialog box में कोई सूचना (message) user को दिखाई जाती है। इसमें केवल OK button होता है।

जब तक user OK button पर click नहीं करता, तब तक आगे की प्रक्रिया (execution) रुकी रहती है।

Syntax

alert("Message");

Example

<!DOCTYPE html>
<html>
<head>
<title>JavaScript Alert Method</title>
</head>
<body style="text-align:center;">
<h1>Arun Computer</h1>
<h2 style="color:red;">Example of JavaScript alert() Method</h2>
<p>Click the button to display an alert box.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
alert("Hello! I am an alert box!");
}
</script>
</body>
</html>

2. prompt() Method

prompt() method का उपयोग user से input प्राप्त करने के लिए किया जाता है। इसे input dialog box भी कहा जाता है।

इस dialog box में एक text field तथा दो buttons — OK और Cancel होते हैं।

  • यदि user OK button दबाता है, तो input value return होती है।
  • यदि user Cancel button दबाता है, तो null return होता है।

Syntax

prompt("Message", "Default Value");

Example

<!DOCTYPE html>
<html>
<head>
<title>JavaScript Prompt Method</title>
</head>
<body style="text-align:center;">
<h1>Arun Computer</h1>
<h2 style="color:red;">Example of JavaScript prompt() Method</h2>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var txt;
var person = prompt("Please enter your name:", "Arun Computer");
if (person == null || person == "") {
txt = "User cancelled the prompt.";
}
else {
txt = "Hello " + person + "! How are you today?";
}
document.getElementById("demo").innerHTML = txt;
}
</script>
</body>
</html>

3. confirm() Method

confirm() method एक confirmation dialog box प्रदर्शित करती है, जिसमें कोई संदेश (message) दिखाया जाता है।

इस dialog box में OK तथा Cancel button होते हैं।

  • यदि user OK button दबाता है, तो method true return करती है।
  • यदि user Cancel button दबाता है, तो method false return करती है।

Syntax

confirm("Message");

Example

<!DOCTYPE html>
<html>
<head>
<title>JavaScript Confirm Method</title>
</head>
<body style="text-align:center;">
<h1>Arun Computer</h1>
<h2 style="color:red;">Example of JavaScript confirm() Method</h2>
<p>Click the button to display a confirm box.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var txt;
var r = confirm("Press a button!");
if (r == true) {
txt = "You pressed OK!";
}
else {
txt = "You pressed Cancel!";
}
document.getElementById("demo").innerHTML = txt;
}
</script>
</body>
</html>

Important Points

  • alert() केवल message दिखाता है।
  • prompt() user से input लेता है।
  • confirm() user से confirmation प्राप्त करता है।
  • ये सभी methods JavaScript के built-in window object का भाग हैं।
  • इन methods को window.alert(), window.prompt() तथा window.confirm() भी लिखा जा सकता है।

Exam Questions – MCU 2019

JavaScript Dialog Boxes क्या हैं? इनके प्रकार लिखिए।

निम्नलिखित Events को समझाइए –

OnLoad

OnUnload

OnMouseOver

OnClick

JavaScript में alert(), prompt() तथा confirm() methods को उदाहरण सहित समझाइए।