What is DOM (ft. chatgpt)
window object (ft. chatgpt)
Window Object is the super parent of all the java script object.
All global JavaScript objects, functions, and variables automatically become members of the window object.
Note : Document Object is a part of Window Object
Example –
alert("Hello");
// is the same as
window.alert("Hello");
Guess the output –
var a = 10;console.log(window.a);let b = 20;console.log(window.b);functionabc(){ var c = 30;};console.log(abc());
There are many property. Let see few important ones
//innerHeight and innerWidth
console.log(window.innerHeight);
console.log(window.innerWidth);
console.log(window.outerHeight);
console.log(window.outerWidth);
console.log(window.scrollX);
console.log(window.scrollY);
window.scrollBy(100, 100);
alert(window.scrollX + window.scrollY);
//open in new tab
window.open("https://www.w3schools.com");
//open in same tab with click
const btnOpen = document.getElementById("btnOpen");
const btnClose = document.getElementById("btnClose");
var win;
btnOpen.addEventListener("click", function () {
win = window.open("https://www.w3schools.com", "", "width=200,height=100");
});
//close a particular window
btnClose.addEventListener("click", function () {
win.close();
});
sessionStorage
//store data in session
sessionStorage.setItem("firstname", "Jhon");
sessionStorage.setItem("lastname", "Smith");
//retrieve data from session
var a = sessionStorage.getItem("lastname");
console.log(a);
//remove data from session
sessionStorage.removeItem("firstname");
sessionStorage.clear();
localStorage
//store data in session
localStorage.setItem("firstname", "Jhon");
localStorage.setItem("lastname", "Smith");
//retrieve data from session
var a = localStorage.getItem("lastname");
console.log(a);
//remove data from session
localStorage.removeItem("firstname");
localStorage.clear();
setTimeout – calls a function after a specified time (for once)
setTimeout(() => {
console.log("b");
}, 2000);
setInterval – calls a function after a specified interval
setInterval(() => {
console.log("c");
}, 2000);
history and location
history –
window.history.back();
window.history.forward();
location –
window.location.reload()
window.location.pathname()
window.location.port()
window.location.assign("https://www.w3schools.com")
window.location.hostname()
window.location.href / window.location.toString()
DOM is an object representation of HTML structure.
Window or global object is a super parent object which provides per-defined properties to play with.
Advertisement