Important Notice:

Confirm box

Confirm box

35 views 2 min read

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()):

  1. कोई खतरनाक काम करने से पहले पूछना
    जैसे – “क्या आप सच में यह फ़ाइल डिलीट करना चाहते हैं?”
  2. लॉगआउट करने से पहले पुष्टि लेना
    जैसे – “क्या आप लॉगआउट करना चाहते हैं?”
  3. फॉर्म रीसेट करने से पहले पूछना
    जैसे – “क्या आप सभी एंट्री हटाना चाहते हैं?”
  4. किसी काम को जारी रखना है या नहीं, यह पूछना
    जैसे – “क्या आप अगले पेज पर जाना चाहते हैं?”
  5. पेज छोड़ने (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():

  1. Ask before a dangerous action
    Example: "Do you really want to delete this file?"
  2. Confirm before logging out
    Example: "Do you want to log out?"
  3. Ask before resetting a form
    Example: "Do you want to clear all entries?"
  4. Ask whether to continue or not
    Example: "Do you want to go to the next page?"
  5. 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>

Related Notes