Skip to content

Commit ea61638

Browse files
committed
Error: Improved image example
Fixes gh-413 Closes gh-630
1 parent 4746900 commit ea61638

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

entries/error.xml

+6-5
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,23 @@ $( "#book" )
3434
})
3535
.attr( "src", "missing.png" );
3636
</code></pre>
37-
<p>If the image cannot be loaded (for example, because it is not present at the supplied URL), the alert is displayed:</p>
37+
<p>If the image cannot be loaded (for example, because it is not present at the supplied URL), the alert is displayed:</p>
3838
<p>
3939
<samp>Handler for .error() called.</samp>
4040
</p>
4141
<div class="warning">
42-
<p>The event handler <em>must</em> be attached before the browser fires the error event, which is why the example sets the src attribute after attaching the handler. Also, the error event may not be correctly fired when the page is served locally; <code>error</code> relies on HTTP status codes and will generally not be triggered if the URL uses the <code>file:</code> protocol.</p>
42+
<p>The event handler <em>must</em> be attached before the browser fires the <code>error</code> event, which is why the example sets the <code>src</code> attribute after attaching the handler. Also, the <code>error</code> event may not be correctly fired when the page is served locally; <code>error</code> relies on HTTP status codes and will generally not be triggered if the URL uses the <code>file:</code> protocol.</p>
4343
</div>
44-
<p>Note: A jQuery error event handler should not be attached to the window object. The browser fires the window's error event when a script error occurs. However, the window error event receives different arguments and has different return value requirements than conventional event handlers. Use <code>window.onerror</code> instead.</p>
44+
<p>Note: A jQuery <code>error</code> event handler should not be attached to the <code>window</code> object. The browser fires the <code>window</code>'s <code>error</code> event when a script error occurs. However, the window <code>error</code> event receives different arguments and has different return value requirements than conventional event handlers. Use <code>window.onerror</code> instead.</p>
4545
</longdesc>
4646
<note id="detach-shorthand" type="additional" data-event="error"/>
4747
<example>
48-
<desc>To hide the "broken image" icons for IE users, you can try:</desc>
48+
<desc>To replace all the missing images with another, you can update the <code>src</code> attribute inside the callback passed to <code>.error()</code>. Be sure that the replacement image exists; otherwise the <code>error</code> event will be triggered indefinitely.</desc>
4949
<code><![CDATA[
50+
// If missing.png is missing, it is replaced by replacement.png
5051
$( "img" )
5152
.error(function() {
52-
$( this ).hide();
53+
$( this ).attr( "src", "replacement.png" );
5354
})
5455
.attr( "src", "missing.png" );
5556
]]></code>

0 commit comments

Comments
 (0)