Modify ↓
Ticket #5315 (closed bug: invalid)
bind and attr error on IE8 and IE7
| Reported by: | ecentinela | Owned by: | |
|---|---|---|---|
| Priority: | major | Milestone: | 1.4 |
| Component: | core | Version: | 1.3.2 |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
Description
This code fails
var $img = $(new Image).load(function() {
alert($img)
}).attr('src', 'image.jpg');
But this not
var $img = $(new Image).load(function() {
alert($img)
});
$img.attr('src', 'image.jpg');
In the first example, alert shows undefined. In the second examples, it returns the jquery node.
This fails in IE 7 and IE 8. IE 6 seems that is OK.
Change History
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.
Note: See
TracTickets for help on using
tickets.

I don't think this is a jQuery bug, although it's an interesting case. It appears that when image.jpg is on your local drive, IE synchronously loads it and fires the onload event before it returns from the chained function. So, $img is not assigned the value by the time the event fires. If you replace the .attr() with an image that must be retrieved from the network, the request is synchronous and behaves the way you expect.