Ghost
King Coder
Hello all,
Today I want to quickly show you my custom ajax() function that uses core JavaScript.
I am slowly creating functions with JS that make using core JS easier for a site, without adding in all of the bloat from a library like jQuery.
I started off with an AJAX function because I have noticed that a lot of our sites use jQuery for two reasons: Bootstrap requirements & AJAX.
However, not all of our sites (that we make for clients, or for ourselves) use Bootstrap, but many of our developers have gotten so accustomed to using jQuery that they want to use it even when a project really doesn't need it... So, I've started making functions that take care of annoying things like AJAX & modal pop ups for projects that really don't need jQuery, but could benefit from some easy to use functions
I don't want to go through every single line, so this won't be labeled as a tutorial, but you can feel free to use the function in your own projects
As you can see, the first block of code is the full AJAX request, which is a lot easier to use as a function. You can just use the function (2nd code block below) to make your life a lot easier, without using a big library such as jQuery to do so
Current code:
github.com
Example:
github.com
Today I want to quickly show you my custom ajax() function that uses core JavaScript.
I am slowly creating functions with JS that make using core JS easier for a site, without adding in all of the bloat from a library like jQuery.
I started off with an AJAX function because I have noticed that a lot of our sites use jQuery for two reasons: Bootstrap requirements & AJAX.
However, not all of our sites (that we make for clients, or for ourselves) use Bootstrap, but many of our developers have gotten so accustomed to using jQuery that they want to use it even when a project really doesn't need it... So, I've started making functions that take care of annoying things like AJAX & modal pop ups for projects that really don't need jQuery, but could benefit from some easy to use functions
I don't want to go through every single line, so this won't be labeled as a tutorial, but you can feel free to use the function in your own projects
As you can see, the first block of code is the full AJAX request, which is a lot easier to use as a function. You can just use the function (2nd code block below) to make your life a lot easier, without using a big library such as jQuery to do so
Current code:
NerdiOrg/wuJS
Common JS functions without the bloat of jQuery. Contribute to NerdiOrg/wuJS development by creating an account on GitHub.

JavaScript:
function ajax(object){
if(object.method.length > 0 && typeof object.method != undefined){
var Request = new XMLHttpRequest();
if(typeof object.url != 'undefined' && object.url.length > 0 && typeof object.method != 'undefined' && object.method.length > 0){
if(typeof object.data === "object"){
var encodeURIString = "";
for (var objProperty in object.data){
if (object.data.hasOwnProperty(objProperty)){
if(encodeURIString.length > 0){
encodeURIString += '&';
}
encodeURIString += encodeURI(objProperty + '=' + object.data[objProperty]);
}
}
} else {
encodeURIString = "";
}
if(object.method === "POST"){
Request.open(object.method, object.url);
Request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
Request.send(encodeURIString);
} else if(object.method === "GET"){
if(encodeURIString != ""){
object.url = object.url + "?" + encodeURIString;
}
Request.open(object.method, object.url);
Request.send();
} else {
console.log("The AJAX Method must be either 'POST' or 'GET'");
return false;
}
} else if(typeof object.url == 'undefined' || object.url.length === 0){
console.log("The AJAX URL cannot be empty or undefined.");
return false;
} else if(typeof object.method == 'undefined' || object.method.length === 0){
console.log("The AJAX Method cannot be empty or undefined.");
return false;
} else {
console.log("The AJAX Method must be either 'POST' or 'GET'");
return false;
}
Request.onload = function(){
if(Request.status === 200){
if(typeof object.success == "function"){
object.success(Request.responseText);
} else {
console.log("The XMLHttpRequest succeeded, but no success function was supplied to the ajax() function.");
}
} else {
if(typeof object.fail == "function"){
object.fail(Request.statusText);
} else {
console.log("The XMLHttpRequest failed, but no fail function was supplied to the ajax() function.");
}
}
} // onload func
}
}
Example:
NerdiOrg/wuJS
Common JS functions without the bloat of jQuery. Contribute to NerdiOrg/wuJS development by creating an account on GitHub.

JavaScript:
ajax({
method: "POST",
url: "sendmsg.php",
data: {
message: 'hi',
userid: 5
},
success: function(data){
console.log(data);
},
fail: function(data){
alert(data);
}
});