Created
December 10, 2010 05:35
-
-
Save redoPop/735829 to your computer and use it in GitHub Desktop.
Replacement jQuery.load() for use with innerShiv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// jQuery plugin based on .load() for use with innerShiv | |
// http://jdbartlett.github.com/innershiv for more info | |
// $('selector').loadShiv('example.html selector'); | |
jQuery.fn.loadShiv = function (url, params, callback) { | |
var off, selector, self, type; | |
if (!this.length || typeof url !== 'string') { | |
return this; | |
} | |
off = url.indexOf(' '); | |
if (off >= 0) { | |
selector = url.slice(off, url.length); | |
url = url.slice(0, off); | |
} | |
type = 'GET'; | |
if (params) { | |
if (jQuery.isFunction(params)) { | |
callback = params; | |
params = null; | |
} else if (typeof params === 'object') { | |
params = jQuery.param(params, jQuery.ajaxSettings.traditional); | |
type = 'POST'; | |
} | |
} | |
self = this; | |
jQuery.ajax({ | |
url: url, | |
type: type, | |
dataType: 'html', | |
data: params, | |
complete: function (res, status) { | |
var shivved; | |
if (status === 'success' || status === 'notmodified') { | |
shivved = jQuery(innerShiv((selector ? '<div>' : '') + res.responseText, false)); | |
if (selector) { | |
shivved = shivved.find(selector); | |
} | |
self.empty().append(shivved); | |
} | |
if (callback) { | |
self.each(callback, [res.responseText, status, res]); | |
} | |
} | |
}); | |
return this; | |
} |
Just a note to say that you need to include the innerShiv plugin as well as this gist. Works like a charm though.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you're loading an entire content fragment straight into the document (i.e., if you're
$.load()
ing into an element that's currently attached the shim'd document, and you're not including a selector in$.load()
's url parameter), you don't need to use loadShiv. Ultimately, ajax aside, all that's doing is setting innerHTML on an element that's already attached to the shim'd document, which isn't a problem. innerShiv is only needed when you're setting the innerHTML of an element outside the shim'd document, something jQuery has to do when using a selector on loaded content or when loading content into an element that's not attached to the document.