Ticket #11852 (closed feature: notabug)
Use context parameter in .load
| Reported by: | anonymous | Owned by: | jaubourg |
|---|---|---|---|
| Priority: | low | Milestone: | None |
| Component: | ajax | Version: | 1.7.2 |
| Keywords: | Cc: | jaubourg | |
| Blocking: | Blocked by: |
Description
I know this is very trivial, but it is an enhancement never the less.
The .load function (ajax) assigns this to self on line 7159.
It would be better to use the context parameter of the $.ajax function as follows and then omitting the assignment to self.
jQuery.ajax({ url: url, type: type, dataType: "html", data: params, context: this, // Complete callback (responseText is used internally) complete: function( jqXHR, status, responseText ) { // Store the response as specified by the jqXHR object responseText = jqXHR.responseText; // If successful, inject the HTML into all the matched elements if ( jqXHR.isResolved() ) { // #4825: Get the actual response in case // a dataFilter is present in ajaxSettings jqXHR.done(function( r ) { responseText = r; }); // See if a selector was specified // (Target element is this - coming from context) this.html( selector ? // Create a dummy div to hold the results jQuery("<div>") // inject the contents of the document in, removing the scripts // to avoid any 'Permission Denied' errors in IE .append(responseText.replace(rscript, "")) // Locate the specified elements .find(selector) : // If not, just inject the full result responseText ); } if ( callback ) { this.each( callback, [ responseText, status, jqXHR ] ); } } });
Patch: http://pastebin.com/A5cYSQKV
In case you are interested where this could have any relevance see this: http://stackoverflow.com/a/10864327/1238764
Change History
comment:1 Changed 11 months ago by dmethvin
- Cc jaubourg added
- Priority changed from undecided to low
- Status changed from new to open
- Component changed from unfiled to ajax
- Milestone changed from None to 1.8
comment:2 Changed 11 months ago by dmethvin
- Owner set to jaubourg
- Status changed from open to assigned
comment:5 Changed 9 months ago by dmethvin
- Type changed from enhancement to feature
Bulk change from enhancement to feature.
comment:6 Changed 6 months ago by dmethvin
- Status changed from assigned to closed
- Resolution set to notabug
Since this would change the callbackContext of the global events fired by $.ajax I think it's better not to land it just in case some code would be confused by the change.
https://github.com/jquery/jquery/blob/67df705bf5205fa075ba6ceee2d7fd4763894a16/src/ajax.js#L358
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

Seems like a good idea to me ... jaubourg?