JavaScript Browser Object Model - Exercise 1

It represents the browser window and provides access to browser-level features.

javascript

console.log(window.innerWidth);

Read window.location.href when you need the complete address of the current document. It returns a string containing the protocol, host, path, and query string.

javascript

const currentUrl = window.location.href;

It navigates the browser to a new URL and keeps the current page in history.

javascript

location.assign('/tutorials/');

Call location.reload() to request the browser to load the current document again. This is useful for a refresh control, although normal links are usually better for navigation.

javascript

button.addEventListener('click', () => {
  location.reload();
});

Call history.back() to move to the previous entry in the browser session history. It behaves like pressing the browser Back button when a previous entry exists.

javascript

history.back();

Use setTimeout with a callback and a delay in milliseconds.

javascript

setTimeout(() => {
  console.log('Later');
}, 1000);

Store the interval ID and pass it to clearInterval.

javascript

const id = setInterval(updateClock, 1000);
clearInterval(id);

It reports the browser's preferred language as a locale tag such as en-US or fr-FR. This is a hint for choosing defaults, not proof that the user speaks only that language.

javascript

console.log(navigator.language);

It describes the physical screen width in CSS pixels, while window.innerWidth describes the page viewport. The values can differ when the browser window does not fill the screen.

javascript

console.log(screen.width);

Different browsers, privacy settings, and non-browser environments may not support every API. Check for the feature before calling it so unsupported functionality can fall back gracefully.

javascript

if ('geolocation' in navigator) {
  console.log('Location is available');
}
All sections expanded. Activate the control to collapse all sections.