Document Properties
Document Properties
The document object has many useful properties!
Common Properties
| Property | What It Returns |
|---|---|
document.title | Page title (string) |
document.URL | Current URL (string) |
document.domain | Domain name |
document.body | The <body> element |
document.head | The <head> element |
document.forms | All <form> elements |
document.images | All <img> elements |
document.links | All <a> elements |
Examples
// Get page title
console.log(document.title);
// Check URL
console.log(document.URL);
// Count images on page
console.log(document.images.length);
// Check if on localhost
const isLocal = document.URL.includes("localhost");
String Methods Work!
Since many properties return strings, you can use string methods:
// Check domain
document.URL.includes("github"); // true/false
// Get title length
document.title.length;
// Uppercase title
document.title.toUpperCase();
Your Turn!
Check if the document URL contains "localhost" and store the result in isLocal.