Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function ge(el) {
- return isElement(el) ? el : document.getElementById(el);
- }
- function $(q, el) {
- return (el || document).querySelector(q);
- }
- function $$(q, el) {
- return (el || document).querySelectorAll(q);
- }
- function clone(o) {
- if (o == null || typeof o != 'object') {
- return o;
- }
- var copy = o.constructor();
- for (var i in o) {
- copy[i] = clone(o[i]);
- }
- return copy;
- }
- function isElement(o) {
- return o instanceof HTMLElement;
- }
- function isArray(o) {
- return o instanceof Array;
- }
- function isObject(o) {
- return o === Object(o);
- }
- function val(el, v) {
- el = ge(el);
- var i = /^(INPUT|TEXTAREA)$/.test(e.tagName) ? 'value': 'innerHTML';
- return arguments.length > 1 ? el[i] = v : el[i];
- }
- function rand(min, max) {
- return Math.floor(Math.random() * (max - min + 1) + min);
- }
- function shuffle(a) {
- for(var i = 0, e = a.length - 1, r, v; i <= e; ++i) {
- r = rand(0, e);
- v = a[r];
- a[r] = a[i];
- a[i] = v;
- }
- return a;
- }
- function choice(a) {
- return a[rand(0, a.length - 1)];
- }
- function strRepeat(s, n) {
- return Array(++n).join(s);
- }
- function strPad(s, len, pad, direction) {
- if (s.length < len) {
- len = len - s.length;
- pad = typeof pad == 'number' || pad ? pad + '' : ' ';
- direction = direction ? direction.toLowerCase() : 'left';
- if (direction == 'both') {
- return strPad('', Math.floor(len / 2), pad) +
- s + strPad('', Math.ceil(len / 2), pad, 'right');
- }
- pad = strRepeat(pad, Math.ceil(len / pad.length)).substr(0, len);
- s = direction == 'right' ? s + pad : pad + s;
- }
- return s;
- }
- function strRev(s) {
- return s.split(/|/).reverse().join('');
- }
- /* ----------------------------( 80 chars line )----------------------------- */
- function escapeHtml(s) {
- return s.replace(/[<>&"]/g, function (i) {
- return '&' + {'<': 'lt', '>': 'gt', '&': 'amp', '"': 'quot'}[i] + ';';
- })
- }
- function escapePattern(s) {
- return s.replace(/([.\\\/+*?[^\]$(){}=!<>|:-])/g, '\\$1');
- }
- var ajax = new Function;
- ajax.getReq = function (m, u, cb) {
- var r = new XMLHttpRequest;
- r.open(m, u);
- r.onload = function () {
- typeof cb == 'function' && cb(r);
- };
- r.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
- return r;
- };
- ajax.get = function (u, cb) {
- var r = this.getReq('GET', u, cb);
- r.send();
- };
- ajax.load = function (u, el) {
- this.get(u, function (r) {
- val(el, r.response);
- });
- };
- ajax.post = function (u, cb, params) {
- var r = this.getReq('POST', u, cb), q = '', i;
- r.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
- for(i in params) {
- if (params.hasOwnProperty(i)) {
- q += (q.length ? '&': '') + i + '=' + encodeURIComponent(params[i]);
- }
- }
- r.send(q);
- };
- ajax.sendForm = function (el, cb) {
- el = ge(el);
- var r = this.getReq('POST', el.action, cb), fd = new FormData(el);
- r.send(fd);
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement