|
| 1 | +# jQuery vs JavaScript Syntax Guide 🚀 |
| 2 | + |
| 3 | +## Introduction |
| 4 | +This guide explains the syntax differences between `window.onload` in JavaScript and `$(document).ready()` in jQuery. We'll dive into the key concepts, practical use cases, and provide real-life examples for both approaches. 📝 |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +## 1. `window.onload` in JavaScript |
| 9 | + |
| 10 | +The `window.onload` event triggers when the entire page, including all assets (images, iframes, stylesheets), has fully loaded. |
| 11 | + |
| 12 | +```javascript |
| 13 | +window.onload = function () { |
| 14 | + console.log("The page and all assets have loaded completely."); |
| 15 | + document.querySelector("p").style.color = "blue"; // Change the color of paragraph text |
| 16 | +}; |
| 17 | +``` |
| 18 | +### When to Use |
| 19 | + |
| 20 | +- **`window.onload`**: |
| 21 | + Use `window.onload` when your script depends on all resources being fully loaded (e.g., large images, external scripts, or iframes). This ensures everything on the page, including assets, is available before your code executes. |
| 22 | +--- |
| 23 | + |
| 24 | +## 2. `$(document).ready()` in jQuery |
| 25 | +In jQuery, the $(document).ready() event runs as soon as the HTML document’s DOM is ready, without waiting for all external resources to load. |
| 26 | + |
| 27 | +```javascript |
| 28 | +$(document).ready(function () { |
| 29 | + console.log("DOM is ready!"); |
| 30 | + $("p").css("color", "red"); // Change paragraph color once DOM is ready |
| 31 | +}); |
| 32 | +``` |
| 33 | + |
| 34 | +### Shorter Version: |
| 35 | +```javascript |
| 36 | +$(function () { |
| 37 | + console.log("DOM is ready with the short jQuery syntax!"); |
| 38 | + $("p").hide(); // Hide paragraphs as soon as the DOM is ready |
| 39 | +}); |
| 40 | +``` |
| 41 | + |
| 42 | +### When to Use: |
| 43 | +Use `$(document).ready()` when you want to manipulate DOM elements as soon as they are loaded, without waiting for images or iframes. |
| 44 | + |
| 45 | +--- |
| 46 | + |
| 47 | +## 3. Using ES6 Arrow Functions with jQuery |
| 48 | + |
| 49 | +ES6 introduced arrow functions for more concise code. You can use them with jQuery for cleaner syntax. |
| 50 | + |
| 51 | +```javascript |
| 52 | +$(document).ready(() => { |
| 53 | + console.log("DOM ready with ES6 arrow function!"); |
| 54 | + $("p").css("color", "green"); // Change paragraph text to green |
| 55 | +}); |
| 56 | +``` |
| 57 | + |
| 58 | +### Shorter Version with ES6: |
| 59 | +```javascript |
| 60 | +$(() => { |
| 61 | + console.log("Short ES6 syntax with jQuery!"); |
| 62 | + $("p").hide(); // Hide paragraphs |
| 63 | +}); |
| 64 | +``` |
| 65 | +## Key Points to Remember 📌 |
| 66 | + |
| 67 | +| Feature | `window.onload` | `$(document).ready()` | |
| 68 | +|----------------------|--------------------------------------------------------|-----------------------------------------------------| |
| 69 | +| **Waits for all resources** | Yes (including images, scripts, etc.) | No (only waits for the HTML document to load) | |
| 70 | +| **When to use** | Use when you need to ensure everything is fully loaded (e.g., images) | Use when you want to manipulate the DOM as soon as it’s available | |
| 71 | + |
| 72 | +# 4. Practical Examples |
| 73 | +## Example 1: Changing background color of `<div>` after DOM is ready |
| 74 | +```javascript |
| 75 | +$(document).ready(function () { |
| 76 | + $("div").css("background-color", "#FF9800"); // Changes background to orange |
| 77 | +}); |
| 78 | +``` |
| 79 | +## Example 2: Hiding an image after DOM is ready |
| 80 | +```javascript |
| 81 | +$(document).ready(function () { |
| 82 | + $("img").hide(); // This hides all images on the page immediately |
| 83 | +}); |
| 84 | +``` |
| 85 | +## Example 3: Display an alert once the entire page is fully loaded |
| 86 | +```javascript |
| 87 | +window.onload = function () { |
| 88 | + alert("Page is fully loaded with all images!"); |
| 89 | +}; |
| 90 | +``` |
| 91 | +## Example 4: Animating a div with jQuery |
| 92 | +```javascript |
| 93 | +$(function () { |
| 94 | + $("div").fadeOut(2000); // This will fade out the div over 2 seconds once the DOM is ready |
| 95 | +}); |
| 96 | +``` |
| 97 | +## Example 5: Working with classes in jQuery |
| 98 | +```javascript |
| 99 | +$(document).ready(() => { |
| 100 | + $("button").click(() => { |
| 101 | + $("p").addClass("highlighted"); |
| 102 | + }); |
| 103 | +}); |
| 104 | +``` |
| 105 | +--- |
| 106 | + |
| 107 | +## Conclusion 🎯 |
| 108 | + |
| 109 | +- **`window.onload`** is great when your code depends on all page assets being fully loaded. |
| 110 | +- **`$(document).ready()`** is much faster for DOM manipulation as it fires as soon as the HTML is parsed. |
| 111 | +- **ES6 arrow functions** give you cleaner, more concise syntax in your code. |
| 112 | + |
| 113 | +Feel free to play around with the examples to see how these events work in practice! |
0 commit comments