G
getsanjay.sharma
Hello to all Javascript programmers out there.
I have written a small Ajax module which makes use of Object literals
to do it's work. It is split into two files, an Ajax.js which contains
all the ajax related functionality and a 'web page' specific .js file
which contains the logic required for the page and which makes use of
'Ajax.js'. Though looking at my implementation I feel as though there
is a lot of coupling. I would like to get your views regarding this
approach used by me.
Any suggestions on what changes I would make to this framework to make
it any better?
AJAX.js
var AJAX =
{
xhr: null,
method: "GET",
url: "",
createXHRObject : function()
{
if(window.ActiveXObject)
return(new ActiveXObject("Microsoft.XMLHTTP"));
else
return(new XMLHttpRequest());
},
setParameters: function(url, method)
{
if(typeof url != 'undefined' && url != null) this.url = url;// + "?
timeStamp=" + new Date().getTime();
if(typeof method != 'undefined' && method != null) this.method =
method;
},
getResponse : function(url, method, handler)
{
alert('h');
this.setParameters(url, method);
this.xhr = this.createXHRObject();
handler['xhr'] = this.xhr;
this.xhr.onreadystatechange = handler;
this.xhr.open(this.method, this.url, true);
this.xhr.send(null);
}
};
SomeForm.js
var SS =
{
setAttributes: function(elem, type, statusId)
{
this.handler['elem'] = elem;
this.handler['type'] = type;
this.handler['statusElem'] = document.getElementById(statusId);
},
/** The reason we can't use 'this' inside handler since it would not
refer to the properties of SS but to the function handler since it is
used as a callback function and inside callbacks, this refers to the
function itself.
*/
handler : function()
{
var XHR = XHR || this['xhr'];
var elem = elem || this['elem'];
var type = type || this['type'];
var statusElem = statusElem || this['statusElem'];
if(XHR.readyState == 4)
{
if(XHR.status == 200)
{
var result = XHR.responseText;
var target = elem.nextSibling;
statusElem.innerHTML = 'Done';
if(result === 'true')
target.innerHTML = "Successful";
else
target.innerHTML = result;
}
else
{
statusElem.innerHTML = 'Invalid url specified / Resource not
found';
}
}
},
/* It is necessary to encode all the input which comes from teh user
since any invalid character will cause the query string to go bad and
the fetching ofdata from the server fail. In short if you are
constructing your querystring based on teh data fetched by the user,
encodeURIComponent is a must!!! */
validate : function(elem, type, statusId)
{
this.setAttributes(elem, type, statusId);
var url = "?do=validate&type=" + encodeURIComponent(type)
+ "&value=" + encodeURIComponent(elem.value);
AJAX.getResponse(url, null, this.handler);
}
};
Any pointers, suggestions to make this better would really be
appreciated.
Thanks and regards,
S T S
I have written a small Ajax module which makes use of Object literals
to do it's work. It is split into two files, an Ajax.js which contains
all the ajax related functionality and a 'web page' specific .js file
which contains the logic required for the page and which makes use of
'Ajax.js'. Though looking at my implementation I feel as though there
is a lot of coupling. I would like to get your views regarding this
approach used by me.
Any suggestions on what changes I would make to this framework to make
it any better?
AJAX.js
var AJAX =
{
xhr: null,
method: "GET",
url: "",
createXHRObject : function()
{
if(window.ActiveXObject)
return(new ActiveXObject("Microsoft.XMLHTTP"));
else
return(new XMLHttpRequest());
},
setParameters: function(url, method)
{
if(typeof url != 'undefined' && url != null) this.url = url;// + "?
timeStamp=" + new Date().getTime();
if(typeof method != 'undefined' && method != null) this.method =
method;
},
getResponse : function(url, method, handler)
{
alert('h');
this.setParameters(url, method);
this.xhr = this.createXHRObject();
handler['xhr'] = this.xhr;
this.xhr.onreadystatechange = handler;
this.xhr.open(this.method, this.url, true);
this.xhr.send(null);
}
};
SomeForm.js
var SS =
{
setAttributes: function(elem, type, statusId)
{
this.handler['elem'] = elem;
this.handler['type'] = type;
this.handler['statusElem'] = document.getElementById(statusId);
},
/** The reason we can't use 'this' inside handler since it would not
refer to the properties of SS but to the function handler since it is
used as a callback function and inside callbacks, this refers to the
function itself.
*/
handler : function()
{
var XHR = XHR || this['xhr'];
var elem = elem || this['elem'];
var type = type || this['type'];
var statusElem = statusElem || this['statusElem'];
if(XHR.readyState == 4)
{
if(XHR.status == 200)
{
var result = XHR.responseText;
var target = elem.nextSibling;
statusElem.innerHTML = 'Done';
if(result === 'true')
target.innerHTML = "Successful";
else
target.innerHTML = result;
}
else
{
statusElem.innerHTML = 'Invalid url specified / Resource not
found';
}
}
},
/* It is necessary to encode all the input which comes from teh user
since any invalid character will cause the query string to go bad and
the fetching ofdata from the server fail. In short if you are
constructing your querystring based on teh data fetched by the user,
encodeURIComponent is a must!!! */
validate : function(elem, type, statusId)
{
this.setAttributes(elem, type, statusId);
var url = "?do=validate&type=" + encodeURIComponent(type)
+ "&value=" + encodeURIComponent(elem.value);
AJAX.getResponse(url, null, this.handler);
}
};
Any pointers, suggestions to make this better would really be
appreciated.
Thanks and regards,
S T S