The

( ā¤šā¤ŋā¤‚ā¤ĻāĨ€ + English   magic )

Learning

Community.

( AN '24 Edition )

DOM (Document Object Model) aur BOM (Browser Object Model) web pages ko dynamic aur interactive banate hain. DOM page ke elements (jaise text aur buttons) ko control karta hai, jisse hum unka content aur style change kar sakte hain, jaise button click karne par text badalna. BOM browser ke features (jaise alert box dikhana, URL change karna) ko control karta hai. Simple shabdon mein, DOM page ka content manage karta hai, aur BOM browser ki functionalities control karta hai. JavaScript in dono ko use karke web pages ko smarter aur user-friendly banata hai.

BOM  Dom   JavaScript đŸŽ¯

What is BOM ?

BOM, (Browser Object Model) browser ka ek feature hai jo hume web page aur browser ke environment ke beech interact karne ka mauka deta hai. BOM browser ki properties aur functionalities ko control karne ka tariqa hai, jaise alerts dikhana, URL change karna, ya browser ki history ko manage karna.



⭐ Why do we use BOM ?

BOM ka use tab hota hai jab hume browser-specific tasks karne hote hain, jaise :

POP-UPS DIKHANA - Users ko alert ya confirmation dena.

NAVIGATION CONTROL KRNA - Kisi doosre URL pe le jana ya page reload karna.

SCREEN INFO LENA - Screen size ya resolution check karna.

BROWSER HISTORY MANAGE KARNA - Pichle page par wapas jaana ya forward move karna.



How does BOM help in web development ?

BOM se hume browser ki power milti hai, jo hume zyada control aur flexibility deta hai :

1. User experience better kar sakte hain (e.g. alerts, confirms).
2. Responsive design ke liye screen width aur height check kar sakte hain.
3. Page navigation automate kar sakte hain (redirects).



⭐ Why is BOM important ?

BOM web developers ko browser ke environment ka control deta hai, jo real-world applications me kaafi useful hota hai. Ye dynamic functionalities add karne ke liye zaroori hai, jaise :

a. Redirects
b. Pop-ups
c. Screen responsiveness
d. Navigation management

BOM ke bina web pages static rehte, lekin BOM se hum interactive aur smart web apps bana sakte hain.



BOM Elements


1. Window : The root of all BOM objects, it represents the browser window/tab. Window sabse bada object hai aur BOM ke har element ka base hai. Iske andar saare objects aur methods hote hain.



ex: WINDOW

// Alert box

Window.alert ( "Welcome to BOM!" );


// Check window size

Console.log (window.innerWidth, window.innerHeight);;



2. Document Object : Document HTML ke saath kaam karta hai (DOM ka part, but linked with BOM). Isse web page ka content select aur modify karte hain. Iska kaam hai, page ke content aur structure ko control karna.



ex: DOCUMENT OBJECT

// Change the page title

document.title = "New Title";


// Modify an HTML element

document.getElementById ( "myDiv" ).innerText = "Hello, World!";



3. Location (For URL Management) : Location current page ke URL ke saath kaam karta hai. Iska main usage hai page ko redirect karna, URL fetch karna, ya page ko reload karna.



ex: LOCATION

// Redirect to another page

window.location.href = "https://www.example.com";


// Reload the current page

window.location.reload();



4. History (For Navigation) : History browser ki back aur forward navigation ko control karta hai. Iska usage hai back jaana, forward jaana, ya history ki length check karna.



ex: HISTORY

// Go back to the previous page

window.history.back();


// Go forward to the next page

window.history.forward();



5. Navigator (Browser Information) : Navigator browser aur user device ke details provide karta hai. Iska usage hai browser info fetch karna, user online/offline status check karna, ya cookies enabled hai ya nahi.



ex: NAVIGATOR

// Get the browser name

console.log(navigator.appName);


// Check if the user is online

console.log(navigator.onLine);


// Get the user's operating system

console.log(navigator.platform);



6. Alert, Confirm, Prompt (For User Interaction) : Pop-ups dikhane ke liye use hoti hain. Alert dena, user se confirmation lena, ya input retrieve karna iska kaam hai.



ex: ALERT, CONFIRM, PROMPT

// Show an alert box

alert( "Hello, Welcome to BOM!" );


// Ask for confirmation (Yes/No)

let isConfirmed = confirm( "Are you sure?" );
console.log(isConfirmed);


// Prompt the user for input

let userName = prompt( "What is your name?" );
console.log(userName);



7. LocalStorage and SessionStorage (For Storing Data) : Browser ke andar data store karne ke liye. LocalStorage me data permanently store hota hai aur SessionStorage tab tak rehta hai jab tak browser close na ho.



ex: LOCALSTORAGE, SESSIONSTORAGE

// Save data to LocalStorage

localStorage.setItem( "username", "JohnDoe" );


// Retrieve data from LocalStorage

let username = localStorage.getItem( "username" );
console.log(username);


// Save data to SessionStorage

sessionStorage.setItem( "sessionID", "12345" );


// Retrieve data from SessionStorage

let sessionID = sessionStorage.getItem( "sessionID" );
console.log(sessionID);



8. Screen (Screen Size and Resolution) : Screen object user ke screen ke width aur height ke details provide karta hai. Responsive design aur screen resolution check karne ke liye useful hai.



ex: SCREEN

// Get the screen width

console.log(screen.width);


// Get the screen height

console.log(screen.height);


// Get the screen's color depth

console.log(screen.colorDepth);



9. Timers (setTimeout and setInterval) : Timed actions perform karne ke liye. Delay ke saath action karna ya repeatedly action perform karna iska kaam hai.



ex: TIMERS

// Set a timer to execute code after a delay

setTimeout( function() { console.log( "This message is delayed!" ); }, 2000 );


// Set an interval to repeat code at a fixed interval

setInterval( function() { console.log( "This message repeats every 3 seconds" ); }, 3000 );



10. Fetch (For HTTP Requests) : API se data fetch karne ya HTTP requests send karne ke liye modern and efficient method hai.



ex: FETCH

// Fetch data from a URL (GET request)

fetch( "https://api.example.com/data" ).then( response => response.json() ).then( data => console.log( data ) );


// Send data to a URL (POST request)

fetch( "https://api.example.com/submit", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify( { "name": "John", "age": 30 } )
}).then( response => response.json() ).then( data => console.log( data ) );



11. Geolocation (For User Location) : User ki current geographical location fetch karne ke liye use hota hai. Iska kaam hai latitude aur longitude retrieve karna.



ex: GEOLOCATION

// Get the user's current location (latitude and longitude)

.geolocation.getCurrentPosition( function(position) {
console.log( "Latitude: " + position.coords.latitude );
console.log( "Longitude: " + position.coords.longitude );
},
function(error) { console.log( error ); }
);


// Get continuous location updates

navigator.geolocation.watchPosition( function(position) {
console.log( "Updated Latitude: " + position.coords.latitude );
console.log( "Updated Longitude: " + position.coords.longitude );
},
function(error) { console.log( error ); }
);



12. Window.open and Window.close (For Tabs) : Browser ke naye tabs ya windows ko kholne aur band karne ke liye use hota hai.



ex: WINDOW.OPEN & WINDOW.CLOSE

// Open a new browser tab or window

let newWindow = window.open( "https://www.example.com", "_blank" );
console.log( "New window opened: ", newWindow );


// Close the newly opened window

newWindow.close();

What is DOM ?

DOM , ka matlab hai ( Document Object Model ) jo ek browser ka representation hota hai jo web page ko samajhne aur control karne me help karta hai. Simple shabdon me, DOM ek bridge hai jo HTML document aur JavaScript ke beech ka connection banata hai.
Jab browser kisi HTML page ko load karta hai, toh wo is HTML ko ek tree structure me convert karta hai. Is structure ko hi DOM bolte hain. DOM me HTML ke har tag, text, aur attribute ko ek node ke roop me represent kiya jata hai.


⭐ Why do we use DOM ?

DOM ka use karte hain web page ke content aur design ko dynamically change karne ke liye bina page ko reload kiye. Iske zariye aap kisi bhi HTML element ko select kar sakte ho, usse modify kar sakte ho, ya naye elements add/remove kar sakte ho.
. Web Page ko JavaScript se connect karna : Iske through aap HTML aur CSS ko JavaScript ki help se change kar sakte ho.
. Interactive Features add karna : DOM ki help se aap buttons par click hone par events trigger kar sakte ho, form validation kar sakte ho, ya kisi bhi element ko dynamically change kar sakte ho.
. Dynamic Updates bina Page Reload ke : DOM ki help se aap real-time me page ko update kar sakte ho bina usse reload kiye. Jaise: Chat messages, Notifications, ya Sliders.



⭐ How does DOM help in web development ?

DOM ki help se :
1. Aap interactive web pages bana sakte ho (like buttons click hone par kuch action hona).
2. Dynamic content change kar sakte ho (user input ke hisaab se text ya images update karna).
3. Web animations ya form validation jaisi functionalities implement kar sakte ho.
4. Real-time updates (e.g., live chat messages ya notifications) add kar sakte ho.



⭐ Impoertant Concepts of DOM :

a. Nodes : DOM ke har part ko node kehte hain (e.g., elements, attributes, text).
b. Methods : DOM me predefined methods hain jaise getElementById(), querySelector(), jo elements ko select karne aur modify karne me madad karte hain.
c. Events : User actions (e.g., click, hover, typing) ko handle karne ke liye DOM events use karte hain.



DOM aur BOM web development ke do important tools hain jo web pages ko interactive aur dynamic banate hain. DOM se hum page ke content aur structure ko control karte hain, aur BOM se browser ke features jaise URL, history, aur alert boxes handle karte hain. JavaScript in dono ko use karke ek powerful aur engaging user experience provide karta hai. Bas concepts ko achhe se samajhiye aur practice kijiye, aapke web projects aur zyada smart aur functional banenge.
Happy Coding! 😊