JavaScript Browser Object Model - Exercise 1
It represents the browser window and provides access to browser-level features.
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.
const currentUrl = window.location.href;
It navigates the browser to a new URL and keeps the current page in history.
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.
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.
history.back();
Use setTimeout with a callback and a delay in milliseconds.
setTimeout(() => {
console.log('Later');
}, 1000);
Store the interval ID and pass it to clearInterval.
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.
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.
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.
if ('geolocation' in navigator) {
console.log('Location is available');
}