Confirm box:-
confirm() – “हाँ / नहीं” पूछने वाला बॉक्स
confirm() बॉक्स का उपयोग यूजर से हाँ (OK) या नहीं (Cancel) में जवाब लेने के लिए किया जाता है। यह भी pop‑up window में खुलता है।
- इसमें दो बटन होते हैं – OK और Cancel
- यह एक वैल्यू वापस (return) करता है:
- OK दबाने पर → true
- Cancel दबाने पर → false
- true या false का उपयोग करके आप तय कर सकते हैं कि आगे क्या करना है।
Syntax:-
let variableName = confirm("Message");
confirm() के सामान्य उपयोग (Common Uses of confirm()):
- कोई खतरनाक काम करने से पहले पूछना
जैसे – “क्या आप सच में यह फ़ाइल डिलीट करना चाहते हैं?”
- लॉगआउट करने से पहले पुष्टि लेना
जैसे – “क्या आप लॉगआउट करना चाहते हैं?”
- फॉर्म रीसेट करने से पहले पूछना
जैसे – “क्या आप सभी एंट्री हटाना चाहते हैं?”
- किसी काम को जारी रखना है या नहीं, यह पूछना
जैसे – “क्या आप अगले पेज पर जाना चाहते हैं?”
- पेज छोड़ने (leave page) से पहले चेतावनी देना
जैसे – “आपके बिना सेव किए बदलाव हैं। क्या फिर भी जाना चाहते हैं?”
Example:-
<!DOCTYPE html>
<html>
<body>
<script>
let result = confirm("Do you really want to delete this message?");
if (result == true) {
document.write("Message deleted.");
} else {
document.write("Deletion cancelled.");
}
</script>
</body>
</html>
confirm() – The "Ask Yes/No" Box
The confirm() box is used to get a Yes (OK) or No (Cancel) answer from the user. It also opens in a pop‑up window.
- It has two buttons – OK and Cancel
- It returns a value:
- Click OK → returns true
- Click Cancel → returns false
- You can use this true / false to decide what to do next.
Common Uses of confirm():
- Ask before a dangerous action
Example: "Do you really want to delete this file?"
- Confirm before logging out
Example: "Do you want to log out?"
- Ask before resetting a form
Example: "Do you want to clear all entries?"
- Ask whether to continue or not
Example: "Do you want to go to the next page?"
- Show a warning before leaving the page
Example: "You have unsaved changes. Do you still want to leave?"
Example:
<!DOCTYPE html>
<html>
<body>
<script>
let result = confirm("Do you really want to delete this message?");
if (result == true) {
document.write("Message deleted.");
} else {
document.write("Deletion cancelled.");
}
</script>
</body>
</html>