Skip to content
Permalink
Browse files
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 34f1042902f8dbf133fdc3c62d17e12ec0cb7a26
Showing with 17 additions and 1 deletion.
  1. +17 −1 src/ajax/ajax.js
@@ -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
@@ -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(){

5 comments on commit 34f1042

@pdokas

This comment has been minimized.

Copy link

@pdokas pdokas replied Nov 3, 2010

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

This comment has been minimized.

Copy link
Member

@jdalton jdalton replied Nov 3, 2010

count me curious too

@pdokas

This comment has been minimized.

Copy link

@pdokas pdokas replied Nov 10, 2010

@mdumic

This comment has been minimized.

Copy link

@mdumic mdumic replied Nov 11, 2010

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

This comment has been minimized.

Copy link

@pdokas pdokas replied Nov 11, 2010

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.