Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Ajax() {}
- var ajax = new Ajax();
- (function () {
- 'using strict';
- var proto = Ajax.prototype;
- proto.getRequest = function (method, url, cb) {
- var r = new XMLHttpRequest;
- r.open(method, url);
- r.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
- r.onload = function () {
- typeof cb == 'function' && cb(r.response, r.status, r);
- };
- return r;
- };
- proto.get = function (url, cb) {
- this.getRequest('GET', url, cb).send();
- };
- proto.update = function (url, elem) {
- this.get(url, function (data) {
- val(elem, data);
- });
- };
- proto.post = function (url, params, cb) {
- var r = this.getRequest('POST', url, cb), data = '', p;
- r.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
- for (p in params) {
- if (params.hasOwnProperty(p)) {
- if (data.length) data += '&';
- data += p + '=' + encodeURIComponent(params[p]);
- }
- }
- r.send(data);
- };
- proto.submit = function (frmElem, targetElem) {
- frmElem = ge(frmElem);
- var data = new FormData(frmElem);
- this.getRequest('POST', frmElem.action, function (data) {
- val(targetElem, data);
- }).send(data);
- };
- })();
- // ...
- function ge(el) {
- return isElement(el) ? el : document.getElementById(el);
- }
- function val(el, v) {
- el = ge(el);
- var i = /^(INPUT|TEXTAREA)$/.test(el.tagName) ? 'value': 'innerHTML';
- return arguments.length > 1 ? el[i] = v : el[i];
- }
- function extend(Child, Parent) {
- var F = function () {};
- F.prototype = Parent.prototype;
- Child.prototype = new F();
- Child.prototype.constructor = Child;
- }
- function clone(o) {
- if (o == null || typeof o != 'object')
- return o;
- var copy = o.constructor();
- for (var p in o)
- copy[p] = clone(o[p]);
- return copy;
- }
- function isElement(o) {
- return o instanceof HTMLElement;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement