DOM and Event Handling
DOM and Event Handling
Introduction to DOM
DOM (Document Object Model) is a programming interface for HTML and XML documents. It represents the
page so that programs can change the document structure, style, and content.
Example:
```javascript
```
Internet - Concepts and Overview
Window Object
The **window** object is the global object in a browser that represents the browser window.
Common properties/methods:
Example:
```javascript
```
Internet - Concepts and Overview
History Object
The **history** object contains the URLs visited by the user in the browser window.
Common methods:
Example:
```javascript
```
Internet - Concepts and Overview
Navigator Object
Example properties:
Example:
```javascript
console.log(navigator.userAgent);
```
Internet - Concepts and Overview
Form Object
```javascript
```
Internet - Concepts and Overview
**Frames**:
Frames are used to divide the browser window into multiple sections. JavaScript can access frames using
`window.frames[]`.
**Location**:
Properties/methods:
Example:
```javascript
```
Internet - Concepts and Overview
- `element.setAttribute()`, `getAttribute()`
Example:
```javascript
document.getElementById("myDiv").style.color = "red";
```
Internet - Concepts and Overview
Example:
```javascript
document.getElementById("btn").onclick = function() {
alert("Button clicked");
```
Internet - Concepts and Overview
Loading Events
- `DOMContentLoaded`: Fires when DOM is fully loaded (without waiting for images etc.)
Example:
```javascript
window.onload = function() {
```
Internet - Concepts and Overview
Event Handlers
```html
```
OR:
```javascript
document.getElementById("btn").addEventListener("click", sayHello);
```
Internet - Concepts and Overview
JavaScript Timers
```javascript
setTimeout(function() {
}, 2000);
```
```javascript
setInterval(function() {
}, 1000);
```
Internet - Concepts and Overview
Form Validation
Form validation checks if form inputs are correctly filled before submitting.
Example:
```javascript
function validateForm() {
if (name == "") {
return false;
```
Use validation to prevent empty, invalid, or unsafe data from being submitted.