Jaren

jQuery getJSON Remake

Mar 21st, 2015
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var $ = function (query) {
  2.     var ritem = document.querySelector(query);
  3.     ritem.css = function (key, value) {
  4.         document.querySelector(query).style[key] = value;
  5.     };
  6.     ritem.click = function(handler){
  7.         document.querySelector(query).onclick = handler;
  8.     };
  9.     return ritem;
  10. };
  11. $.getJSON = function (json) {
  12.     var xmlhttp;
  13.  
  14.     if (window.XMLHttpRequest) {
  15.         // code for IE7+, Firefox, Chrome, Opera, Safari
  16.         xmlhttp = new XMLHttpRequest();
  17.     } else {
  18.         // code for IE6, IE5
  19.         xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  20.     }
  21.  
  22.     xmlhttp.onreadystatechange = function() {
  23.         if (xmlhttp.readyState == 4 ) {
  24.            if(xmlhttp.status == 200){
  25.                if (xmlhttp.getResponseHeader("content-type").indexOf('json') > -1){
  26.                    json.success(JSON.parse(xmlhttp.responseText));
  27.                }
  28.            }
  29.         }
  30.     }
  31.  
  32.     xmlhttp.open("GET", json.url, true);
  33.     xmlhttp.send();
  34. };
Advertisement
Add Comment
Please, Sign In to add comment