Browser Object Model | Javascript Hindi Course For Beginners ( 2023 ) #10

 

What is Window Object

 
window object represents the current browser window or tab. It is a global object that provides access to many properties and methods that can be used to interact with the browser environment.
Note that the window object is a global object, which means that you don't need to explicitly reference it in most cases. For example, you could simply write alert("Hello, world!") instead of window.alert("Hello, world!") .
 

Timing methods

setTimeout(): Executes a function after a specified number of milliseconds.
function sayHello() { console.log("Hello, world!"); } setTimeout(sayHello, 3000); // Wait 3 seconds before calling sayHello
clearTimeout()is a JavaScript function that cancels a timeout previously set with the setTimeout()function. It takes one parameter, which is the ID of the timeout to be canceled.
function sayHello() { console.log("Hello, world!"); } let timeoutId = setTimeout(sayHello, 5000); // Wait 5 seconds before calling sayHello // Cancel the timeout clearTimeout(timeoutId);
We can cancel the timeout using clearTimeout()and passing in the timeoutIdvariable as the parameter. This will prevent the sayHello()function from being called after the 5-second delay.
Note that if the timeout has already been executed before clearTimeout() is called, then clearTimeout() has no effect.
It's good practice to always cancel a timeout when it is no longer needed to prevent any unintended consequences or performance issues.
 
setInterval()is a built-in JavaScript function that repeatedly executes a function at a specified interval (in milliseconds) until it is stopped.
function sayHello() { console.log("Hello, world!"); } setInterval(sayHello, 2000); // Call sayHello every 2 seconds //In this example, the setInterval() function will call the sayHello() function every 2 seconds // until it is stopped. The sayHello() function simply logs the message "Hello, world!" to the console.
 
// using clear interval we can stop setInterval let intervalId = setInterval(sayHello, 2000); // Stop the interval after 10 seconds setTimeout(() => { clearInterval(intervalId); }, 10000);
 
Dialog Methods
  • alert(): Displays an alert box with a message and an OK button.
  • prompt(): Displays a dialog box with a message, an input field, and OK and Cancel buttons. Returns the value entered by the user.
  • confirm(): Displays a dialog box with a message and OK and Cancel buttons. Returns a Boolean value indicating whether the user clicked OK or Cancel.
 
 
Location Methods
  • location.href returns the href (URL) of the current page
  • location.hostname returns the domain name of the web host
  • location.pathname returns the path and filename of the current page
  • location.protocol returns the web protocol used (http: or https:)
  • location.assign() loads a new document
<!DOCTYPE html> <html> <body> <h2>JavaScript</h2> <h3>The window.location object</h3> <input type="button" value="Load new document" onclick="newDoc()"> <script> function newDoc() { window.location.assign("https://www.dosomecoding.com") } </script> </body> </html>