In our previous lessons, we've been learning about the Browser Object Model (BOM) and its various components. So far, we've covered the Window object, the Location object, the Navigation object, and more. Now, let's move on to another important part of the BOM: the Screen object.
The Screen object is a part of the BOM that provides information about the user's screen, such as its size and resolution. It's like a report card for the user's screen, giving you details about its capabilities.
Think of the Screen object as a way to get information about the user's screen, which can be useful for designing and developing web pages that look great on different devices.
The Screen object has several properties that provide information about the user's screen. Here are some of the most important ones:
availHeight
: This property returns the height of the screen in pixels, minus the height of any toolbars or other features that take up space on the screen.availWidth
: This property returns the width of the screen in pixels, minus the width of any toolbars or other features that take up space on the screen.colorDepth
: This property returns the color depth of the screen, which is the number of bits used to represent the color of each pixel.height
: This property returns the total height of the screen in pixels.pixelDepth
: This property returns the pixel depth of the screen, which is the number of bits used to represent the color of each pixel.width
: This property returns the total width of the screen in pixels.These properties can be used to get information about the user's screen, which can be useful for designing and developing web pages that look great on different devices.
Let's start by looking at an example that uses the screen
object to access different properties of the user's screen.
<!DOCTYPE html>
<html>
<head>
<title>Screen Object Example</title>
</head>
<body>
<script>
// Get the height of the screen
const screenHeight = screen.height;
console.log(screenHeight);
// Get the width of the screen
const screenWidth = screen.width;
console.log(screenWidth);
// Get the color depth of the screen
const screenColorDepth = screen.colorDepth;
console.log(screenColorDepth);
// Get the pixel depth of the screen
const screenPixelDepth = screen.pixelDepth;
console.log(screenPixelDepth);
// Get the available height of the screen
const screenAvailHeight = screen.availHeight;
console.log(screenAvailHeight);
// Get the available width of the screen
const screenAvailWidth = screen.availWidth;
console.log(screenAvailWidth);
</script>
</body>
</html>
In this example, we're using the screen
object to access and log various properties of the user's screen. Here's what's happening in each step:
screen.height
to get the total height of the screen in pixels and store it in the screenHeight
variable. The value is then logged to the console.screen.width
to get the total width of the screen in pixels and store it in the screenWidth
variable. The value is logged to the console.screen.colorDepth
property returns the number of bits used to represent the color of each pixel, and this value is stored in the screenColorDepth
variable and logged to the console.screen.pixelDepth
property returns the pixel depth of the screen, which is stored in the screenPixelDepth
variable and logged to the console.screen.availHeight
property returns the available height of the screen, which excludes the height taken up by toolbars or other features. This value is stored in the screenAvailHeight
variable and logged to the console.screen.availWidth
property returns the available width of the screen, which excludes the width taken up by toolbars or other features. This value is stored in the screenAvailWidth
variable and logged to the console.By using the screen
object, we can access detailed information about the user's screen, which can be helpful for creating responsive designs that work well on different devices.
We can also use the Screen object to update the content of our webpage dynamically. For instance, let's say we want to display the screen width inside a paragraph on our webpage.
<!DOCTYPE html>
<html>
<head>
<title>Screen Width Example</title>
</head>
<body>
<p id="screen-info"></p>
<script>
// Get the screen width
const screenWidth = screen.width;
// Find the paragraph element by its ID
const paragraph = document.getElementById('screen-info');
// Set the text content of the paragraph to show
// Set the text content of the paragraph to show the screen width
paragraph.textContent = 'Screen Width:'+ screenWidth +'pixels';
</script>
</body>
</html>
In this example:
screen.width
.screen-info
using document.getElementById
.In this lesson, we've learned about the Screen object and its properties. We've also seen how to use the Screen object to get information about the user's screen, and how to update the content of our webpage dynamically.
The Screen object is an important part of the BOM, and understanding how to use it can help you design and develop web pages that look great on different devices.