Skip to content

Instantly share code, notes, and snippets.

@tbranyen
Created August 23, 2010 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tbranyen/546608 to your computer and use it in GitHub Desktop.
Save tbranyen/546608 to your computer and use it in GitHub Desktop.
potential new load filter
// The idea is to create a DOM based off an xml string passed to it. Very very rough draft of what this would look like.
// Working example @ http://tabdeveloper.com/jquery/load/
// Current problems are that <html>,<head>, and <body> tags are currently stripped from load's filter and that external resource
// are requested because the original load uses a 'dummy div'. This code attempts to solve both issues.
jQuery.dom = function(xmlstr) {
var doc;
// Start with standards compliant browsers first since they own
if("DOMParser" in window) {
console.log("dom parser");
return $((new window.DOMParser()).parseFromString(xmlstr, "text/xml"));
}
// Next best thing is creating a document
else if(document.implementation && document.implementation.createDocument) {
console.log("document.implementation");
doc = document.implementation.createDocument("", null, null);
doc.open();
doc.write(xmlstr);
doc.close();
return $(doc);
}
// Dealing with IE most likely now...
var test = $("<xml/>").html(xmlstr).appendTo("body");
doc = test[0].XMLDocument;
if(doc !== null && typeof doc == "object") {
console.log("xml island");
test.remove();
return $(doc);
}
// Urgh just use activex already
else if("ActiveXObject" in window) {
console.log("activex");
doc = new ActiveXObject("Microsoft.XMLDOM");
doc.async = "false";
doc.loadXML(xmlstr);
$("body").html( $(doc).find("title").html() );
return $(doc);
}
console.log("last resort");
// Last resort use an empty <div/>
return $("<div/>").html(xmlstr);
};
$.fn.load = function( url, params, callback ) {
if ( typeof url !== "string" ) {
return _load.call( this, url );
// Don't do a request if no elements are being requested
} else if ( !this.length ) {
return this;
}
var off = url.indexOf(" ");
if ( off >= 0 ) {
var selector = url.slice(off, url.length);
url = url.slice(0, off);
}
// Default to a GET request
var type = "GET";
// If the second parameter was provided
if ( params ) {
// If it's a function
if ( jQuery.isFunction( params ) ) {
// We assume that it's the callback
callback = params;
params = null;
// Otherwise, build a param string
} else if ( typeof params === "object" ) {
params = jQuery.param( params, jQuery.ajaxSettings.traditional );
type = "POST";
}
}
var self = this;
// Request the remote document
jQuery.ajax({
url: url,
type: type,
dataType: "html",
data: params,
complete: function( res, status ) {
// If successful, inject the HTML into all the matched elements
if ( status === "success" || status === "notmodified" ) {
// See if a selector was specified
self.html( selector ?
// inject the contents of the document in, removing the scripts
// to avoid any 'Permission Denied' errors in IE
jQuery.dom(res.responseText.replace(rscript, ""))
// Locate the specified elements
.find(selector) :
// If not, just inject the full result
res.responseText );
}
if ( callback ) {
self.each( callback, [res.responseText, status, res] );
}
}
});
return this;
};
@tbranyen
Copy link
Author

jQuery.dom function could get placed into the initial jQuery.extend potentially below the domReady section (where I placed it in the demo). The load function has just been changed to load in the xml through that dom function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment