jQuery Browser Events

Beginner
⏱️ Hub guide
📚 Updated: Jul 2026
🚀 6 event tutorials

What You’ll Learn

Browser events let your page react when resources load or fail — broken product photos, missing avatars, and failed media embeds. jQuery’s .on("error") attaches handlers the same way as click or keydown. Start with the error event for failed image loads.

01

.on()

Bind handlers

02

error

Load failed

03

Fallback

Swap src

04

Before src

Attach first

05

.trigger()

Fire events

06

6 guides

Full index

Introduction

Every gallery, e-commerce catalog, and user profile page shows images from URLs that might break — deleted files, CDN outages, or typos in the path. Without an error handler, users see the broken-image icon. With jQuery, you can swap in a placeholder the moment loading fails.

What Are jQuery Browser Events?

Browser events are named strings passed to .on()"error" when loading fails, and "load" when loading succeeds (on elements like <img>). The handler receives a standard jQuery event object. The legacy .error() shorthand (removed in jQuery 3.0) bound the same event — use .on("error") since jQuery 1.7 in modern code.

💡
Beginner Tip

Bind .on("error", handler) on <img> elements before setting a broken src. Use a valid fallback URL so the handler does not loop forever. For JavaScript exceptions, use window.onerror — not jQuery on window.

📝 Syntax

Minimal error binding workflow:

jQuery
$("#book")
  .on("error", function() { alert("Handler for error called."); })
  .attr("src", "missing.png");

$("img").on("error", function() {
  $(this).attr("src", "replacement.png");
});

$("#book").trigger("error");

👀 error vs load vs window.onerror vs deprecated .error()

Four related concepts — pick the right tool:

.on("error", fn) on <img> → image URL failed to load — swap fallback src .on("load", fn) on <img> → image loaded successfully window.onerror → uncaught JavaScript exception — not jQuery on window .error() shorthand → removed in jQuery 3.0 — use .on("error") and .trigger("error")

Browser Event Tutorial Index

Search by event name or browse by category. Each tutorial includes syntax, try-it examples, and FAQs.

Resource Loading

2 tutorials

Respond when the browser finishes or fails loading page resources — error on broken images and legacy .error() shorthand.

EventDescriptionTutorial
error eventBind a handler or trigger the error event when an image or media element fails to load — attach before setting src and use a valid fallback.Open
.error()Legacy shorthand removed in jQuery 3.0 — bind or trigger the error event; migrate to .on('error') and .trigger('error').Open

Window Size

2 tutorials

React when the viewport changes — resize fires on window when the user resizes the browser; debounce expensive layout code.

EventDescriptionTutorial
resize eventBind a handler or trigger the resize event when the browser window changes size — debounce handlers and read $(window).width().Open
.resize()Deprecated shorthand since jQuery 3.3 — bind or trigger the resize event on window; migrate to .on('resize') and .trigger('resize').Open

Scrolling

2 tutorials

Detect scroll on window or overflow elements — build sticky headers, progress bars, and infinite-load triggers.

EventDescriptionTutorial
scroll eventBind a handler or trigger the scroll event when the user scrolls window or overflow elements — read scrollTop and throttle handlers.Open
.scroll()Deprecated shorthand since jQuery 3.3 — bind or trigger the scroll event; migrate to .on('scroll') and .trigger('scroll'). Not .scrollTop().Open

Conclusion

jQuery browser events help you handle resource loading gracefully. Start with .on("error", handler) on images — attach before setting src, swap to a valid fallback, and avoid binding error on window for script exceptions.

❓ Frequently Asked Questions

Browser events fire when the browser loads or fails to load page resources — images, scripts, and other elements referenced by the document. jQuery binds them with .on() and .trigger() using the same API as click or keydown events.
The error event is sent to elements such as images when the browser cannot load the resource at the given URL. Bind with .on('error', handler) since jQuery 1.7. Attach the handler before setting src so the browser does not miss the event.
No. The window error event for uncaught JavaScript exceptions has different arguments and return-value rules. Use window.onerror or window.addEventListener('error', ...) for script errors instead of $(window).on('error').
The error event relies on HTTP status codes. Pages served with the file: protocol often do not fire error the same way. Test over http:// or https:// — including the Try-it editor on this site.
Inside the error handler, set $(this).attr('src', 'fallback.png'). Ensure the fallback image exists and loads — otherwise error fires again in an infinite loop. Use .one('error') or remove the handler after swapping.
Read the overview, study the error vs load vs window.onerror comparison, browse the event index, then open the error event tutorial for full syntax, five try-it labs, and event-specific FAQs.

Start with error event

Learn .on("error"), image fallbacks, and .trigger("error").

error event tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful