← Back to JavaScript Course | Chapter 11: Browser Object Model | Lesson 6 of 6

JS पॉपअप बॉक्स

तीनों popup functions एक पुराने rotary phone जैसे हैं: ये काम करते हैं, हर कोई इन्हें तुरंत समझ जाता है, पर call के दौरान ये पूरे कमरे को freeze कर देते हैं और आप इन पर कोई custom wallpaper या ringtone नहीं लगा सकते।
Syntax
javascript
window.alert("message");
const ok = window.confirm("message");
const text = window.prompt("message", "default");

तीन Popup Functions

JavaScript तीन built-in blocking dialogs देता है: alert() एक OK बटन के साथ एक message दिखाता है, confirm() एक yes/no सवाल पूछता है और true या false return करता है, और prompt() text input मांगता है और entered string या cancel होने पर null return करता है।

उदाहरण: The Three Popup Functions

javascript
// Call `document.alert("Message with OK button")`
// Call `document.alert("Message with OK button")`
window.alert("Message with OK button");
// Declare the constant `confirmed`, set to `window.confirm("Are you sure?")`
// Declare the constant `confirmed`, set to `window.confirm("Are you sure?")`
const confirmed = window.confirm("Are you sure?");
// Declare the constant `name`, set to `window.prompt("Enter your name:")`
// Declare the constant `name`, set to `window.prompt("Enter your name:")`
const name = window.prompt("Enter your name:");
// Print `confirmed, name` to the console
// Print `confirmed, name` to the console
console.log(confirmed, name);

Blocking, Synchronous व्यवहार

तीनों popups user के जवाब देने तक script execution को पूरी तरह रोक देते हैं, जो modern JavaScript में असामान्य है जहाँ लगभग बाकी सब कुछ (fetch, timers, event handling) asynchronous है।

जब इनमें से कोई dialog खुला हो तो page पर कोई और code नहीं चलता।

उदाहरण: Blocking, Synchronous Behavior

javascript
console.log("before");
window.alert("Page is paused until you click OK");
console.log("after"); // doesn't log until alert is dismissed

Practice में Return Values

alert() हमेशा undefined return करता है क्योंकि इसमें सिर्फ एक OK बटन होता है, confirm() एक असली boolean return करता है जिस पर आप सीधे branch कर सकते हैं, और prompt() या तो टाइप की गई string या null return करता है, जिसे result इस्तेमाल करने से पहले जांचना ज़रूरी है ताकि cancel को खाली string न समझा जाए।

उदाहरण: Return Values in Practice

javascript
const result = window.alert("Just OK"); // always undefined
console.log(result);
const ok = window.confirm("Proceed?");
console.log(typeof ok); // "boolean"

Styling की सीमाएं

तीनों में से किसी भी dialog को CSS से style, reposition, या उनके text content से आगे customize नहीं किया जा सकता, क्योंकि ये खुद browser chrome द्वारा render होते हैं, page के DOM से नहीं। यह एक कठोर सीमा है, कोई workaround-able सीमा नहीं।

उदाहरण: Styling Limitations

javascript
window.alert("This cannot be styled with CSS or repositioned.");

Production UIs इन्हें क्यों टालते हैं

चूंकि ये पूरे page को block करते हैं और किसी site के visual design से मेल नहीं खा सकते, ज़्यादातर production interfaces इसके बजाय custom modal components बनाते हैं, native popups को ज़्यादातर quick debugging या बहुत कम-stakes वाले internal tools के लिए रखते हुए जहाँ blocking व्यवहार और default styling स्वीकार्य tradeoffs हैं।

उदाहरण: Why Production UIs Avoid Them

javascript
window.alert("Native popups are mostly for quick debugging.");
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. sandboxed iframes के अंदर alert() और confirm() के काम करने की उम्मीद करना, जहाँ वे block हो सकते हैं।
  2. prompt() इस्तेमाल करना और भूल जाना कि यह cancel पर null और बाकी हमेशा एक string return करता है।
  3. important UI के लिए popups इस्तेमाल करना, क्योंकि वे पूरे page को block कर देते हैं और उन्हें style नहीं किया जा सकता।
चैप्टर सारांश
  • Browser Object Model JavaScript को window object के ज़रिए browser तक access देता है।
  • screen, location, history, और navigator objects information और control देते हैं।
  • Popup boxes user से interact करते हैं।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.