Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
New feature: You can now inject portions of a document via .load(), a…
…s opposed to the full thing.

Examples:
- $("#test").load("test.html #something");
- $("#test").load("test.html p.user");

Caveats:
- No scripts are injected when a selector is used.
- The selector is rooted inside the head and body - it's equivalent to doing:
  $("body,head").find(selector)
  • Loading branch information
jeresig committed Aug 25, 2007
1 parent 97f2032 commit 34f1042
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/ajax/ajax.js
Expand Up @@ -48,6 +48,10 @@ jQuery.fn.extend({
if ( jQuery.isFunction( url ) )
return this.bind("load", url);

var off = url.indexOf(" ");
var selector = url.slice(off, url.length);
url = url.slice(0, off);

callback = callback || function(){};

// Default to a GET request
Expand Down Expand Up @@ -78,7 +82,19 @@ jQuery.fn.extend({
complete: function(res, status){
// If successful, inject the HTML into all the matched elements
if ( status == "success" || !ifModified && status == "notmodified" )
self.html(res.responseText);
// See if a selector was specified
self.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(res.responseText.replace(/<script(.|\s)*?\/script>/g, ""))

// Locate the specified elements
.find(selector) :

// If not, just inject the full result
res.responseText );

// Add delay to account for Safari's delay in globalEval
setTimeout(function(){
Expand Down

5 comments on commit 34f1042

@pdokas
Copy link

@pdokas pdokas commented on 34f1042 Nov 3, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm very curious about the prohibition on inline scripts in requested content before insertion into the document. In some testing of this (upon running into it the hard way) I've been unable to generate the suggested Permission Denied errors in IE. Are there any old bugs that this referenced or other form of documentation on the issue being worked around? Some discussion on the topic I've found suggests that if the defer attribute is specified on the script then the permission denied error won't be generated. Just as soon as I can reliably generate such an error I plan to investigate the suggested effects of the defer attr.

@jdalton
Copy link
Member

@jdalton jdalton commented on 34f1042 Nov 3, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

count me curious too

@pdokas
Copy link

@pdokas pdokas commented on 34f1042 Nov 10, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mdumic
Copy link

@mdumic mdumic commented on 34f1042 Nov 11, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not encounter any such errors but script stripping was helpful on few occasions. I had a case where I wanted to avoid reloading the same page but to insert just the parts that changed. When I obtain responseText and create DOM fragment (disconnected) in order to query it for elements I need, IE runs all scripts in the fragment, effectively re-initializing everything I have on the page. The way $.load works was just what I needed.

@pdokas
Copy link

@pdokas pdokas commented on 34f1042 Nov 11, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve encountered that rerunning of scripts (I believe it's more than just IE) as I’ve been working through things and it’s definitely got its good and bad aspects to it. But if the defer attribute steps around that issue, it seems like the script regex should avoid those scripts so that we have a way to allow scripts to opt-out of being inserted into the page.

Please sign in to comment.