function ajax( url, options ){
if ( typeof url === "object" ) {
options = url;
url = undefined;
}
options = options || {};
options.type = options.type || 'get';
options.dataType = options.dataType || 'html';
options.async = options.async === undefined ? true : options.async;
var data = null;
if ( options.data ) {
if ( typeof options.data == "object" ) {
for( var i in options.data ) {
data = ( ( data == null ) ? "" : data + "&" ) + ( i + "=" + options.data[ i ] );
}
} else {
data = options.data;
}
}
url = url || options.url;
url = ( url + "" ).replace( /#.*$/, "" );
var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"),
perhapsJSON, textStatus, error = false;
xhr.onreadystatechange = function() {
if ( xhr.readyState == 4 ) {
//xhr.onreadystatechange = null;
if ( xhr.status >= 200 && xhr.status < 300 || xhr.status === 304 ) {
perhapsJSON = xhr.responseText;
textStatus = xhr.statusText || "";
try {
if ( options.dataType == 'json' ) {
if ( window.JSON && JSON.parse ) {
perhapsJSON = JSON.parse( perhapsJSON );
} else {
perhapsJSON = (new Function( "return " + perhapsJSON ))();
}
}
} catch( _ ) {
error = true;
if ( options.error ) {
options.error.call( xhr, xhr, textStatus );
}
}
if ( !error ) {
if ( options.success ) {
options.success.call( xhr, perhapsJSON, textStatus, xhr );
}
}
if ( options.complete ) {
options.complete.call( xhr, xhr, textStatus );
}
} else {
if ( options.error ) {
options.error.call( xhr, xhr, xhr.statusText || "" );
}
}
xhr = null;
}
}
xhr.open( options.type, url, options.async );
try {
xhr.setRequestHeader( "X-Requested-With", "XMLHttpRequest" );
if ( options.type == "post" ) {
xhr.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
}
} catch( _ ) {}
xhr.send( data );
return xhr;
}
xhr = ajax({
url: href,
type: "get",
dataType: "html",
success: function( data, textStatus, xhr ) {
},
complete: function( xhr, textStatus ) {
},
error: function( xhr, textStatus ) {
}
});