Advertisement
stuppid_bot

Просто функции

Jul 3rd, 2014
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function ge(el) {
  2.     return isElement(el) ? el : document.getElementById(el);
  3. }
  4.  
  5. function $(q, el) {
  6.     return (el || document).querySelector(q);
  7. }
  8.  
  9. function $$(q, el) {
  10.     return (el || document).querySelectorAll(q);
  11. }
  12.  
  13. function clone(o) {
  14.     if (o == null || typeof o != 'object') {
  15.         return o;
  16.     }
  17.     var copy = o.constructor();
  18.     for (var i in o) {
  19.         copy[i] = clone(o[i]);
  20.     }
  21.     return copy;
  22. }
  23.  
  24. function isElement(o) {
  25.     return o instanceof HTMLElement;
  26. }
  27.  
  28. function isArray(o) {
  29.     return o instanceof Array;
  30. }
  31.  
  32. function isObject(o) {
  33.     return o === Object(o);
  34. }
  35.  
  36. function val(el, v) {
  37.     el = ge(el);
  38.     var i = /^(INPUT|TEXTAREA)$/.test(e.tagName) ? 'value': 'innerHTML';
  39.     return arguments.length > 1 ? el[i] = v : el[i];
  40. }
  41.  
  42. function rand(min, max) {
  43.     return Math.floor(Math.random() * (max - min + 1) + min);
  44. }
  45.  
  46. function shuffle(a) {
  47.     for(var i = 0, e = a.length - 1, r, v; i <= e; ++i) {
  48.         r = rand(0, e);
  49.         v = a[r];
  50.         a[r] = a[i];
  51.         a[i] = v;
  52.     }
  53.     return a;
  54. }
  55.  
  56. function choice(a) {
  57.     return a[rand(0, a.length - 1)];
  58. }
  59.  
  60. function strRepeat(s, n) {
  61.     return Array(++n).join(s);
  62. }
  63.  
  64. function strPad(s, len, pad, direction) {
  65.     if (s.length < len) {
  66.         len = len - s.length;
  67.         pad = typeof pad == 'number' || pad ? pad + '' : ' ';
  68.         direction = direction ? direction.toLowerCase() : 'left';
  69.         if (direction == 'both') {
  70.             return strPad('', Math.floor(len / 2), pad) +
  71.                 s + strPad('', Math.ceil(len / 2), pad, 'right');
  72.         }
  73.         pad = strRepeat(pad, Math.ceil(len / pad.length)).substr(0, len);
  74.         s = direction == 'right' ? s + pad : pad + s;
  75.     }
  76.     return s;
  77. }
  78.  
  79. function strRev(s) {
  80.     return s.split(/|/).reverse().join('');
  81. }
  82.  
  83. /* ----------------------------( 80 chars line )----------------------------- */
  84.  
  85. function escapeHtml(s) {
  86.     return s.replace(/[<>&"]/g, function (i) {
  87.         return '&' + {'<': 'lt', '>': 'gt', '&': 'amp', '"': 'quot'}[i] + ';';
  88.     })
  89. }
  90.  
  91. function escapePattern(s) {
  92.     return s.replace(/([.\\\/+*?[^\]$(){}=!<>|:-])/g, '\\$1');
  93. }
  94.  
  95. var ajax = new Function;
  96.  
  97. ajax.getReq = function (m, u, cb) {
  98.     var r = new XMLHttpRequest;
  99.     r.open(m, u);
  100.     r.onload = function () {
  101.         typeof cb == 'function' && cb(r);
  102.     };
  103.     r.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
  104.     return r;
  105. };
  106.  
  107. ajax.get = function (u, cb) {
  108.     var r = this.getReq('GET', u, cb);
  109.     r.send();
  110. };
  111.  
  112. ajax.load = function (u, el) {
  113.     this.get(u, function (r) {
  114.         val(el, r.response);
  115.     });
  116. };
  117.  
  118. ajax.post = function (u, cb, params) {
  119.     var r = this.getReq('POST', u, cb), q = '', i;
  120.     r.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  121.     for(i in params) {
  122.         if (params.hasOwnProperty(i)) {
  123.             q += (q.length ? '&': '') + i + '=' + encodeURIComponent(params[i]);
  124.         }
  125.     }
  126.     r.send(q);
  127. };
  128.  
  129. ajax.sendForm = function (el, cb) {
  130.     el = ge(el);
  131.     var r = this.getReq('POST', el.action, cb), fd = new FormData(el);
  132.     r.send(fd);
  133. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement