Lesson 85 +15 XP

Window, Screen, Location

These objects tell you about the browser and the current page.

The window object

window is the browser window. Since it is the global object, methods are often written without it:

Code

js
window.alert("Hello");
alert("Hello"); // same thing

Common window features

Code

js
window.innerWidth;   // viewport width
window.innerHeight;  // viewport height
window.open(url);    // open a new window
window.close();      // close the window

The screen object

screen describes the user's display:

Code

js
screen.width;   // screen width in pixels
screen.height;  // screen height
screen.availWidth; // usable width

The location object

location holds the current URL and can navigate:

Code

js
location.href;      // the full URL
location.hostname;  // the domain name
location.pathname;  // the path part
location.reload();  // reload the page

TL;DR

  • window is the browser window and global object.
  • screen has display info.
  • location has the URL and navigation.
  • innerWidth/innerHeight read the viewport.