Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. var sys = (function() {
  3.  
  4.     var private_var;
  5.  
  6.     function private_method()
  7.     {
  8.         // coming soon!
  9.     }
  10.  
  11.     return {
  12.         typeOf: function(object,type){
  13.             if (type == null) {
  14.                 switch (typeof object) {
  15.                     case 'undefined':
  16.                         return 'undefined';
  17.                         break;
  18.                     case 'object':
  19.                         return 'object';
  20.                         break;
  21.                     case 'boolean':
  22.                         return 'boolean';
  23.                         break;
  24.                     case 'number':
  25.                         return 'number';
  26.                         break;
  27.                     case 'string':
  28.                         return 'string';
  29.                         break;
  30.                     default:
  31.                         return 'error';            
  32.                 }
  33.             }
  34.             else {
  35.                 if(typeof object == type){
  36.                     return true;
  37.                 }else{
  38.                     return false;
  39.                 }
  40.                
  41.             }
  42.         },
  43.         testXMLHttpRequest: function(){
  44.             var xhr;
  45.             try {
  46.                 xhr = new ActiveXObject('Msxml2.XMLHTTP');
  47.             }catch (err2) {
  48.                 try {
  49.                     xhr = new ActiveXObject('Microsoft.XMLHTTP');
  50.                 }catch (err3) {
  51.                     try {
  52.                         xhr = new XMLHttpRequest();
  53.                     }
  54.                     catch (err1)
  55.                     {
  56.                         return false;
  57.                     }
  58.                 }
  59.             }
  60.             return xhr;
  61.         },
  62.         json : {
  63.             string : function(obj){
  64.                 try {
  65.                     var newObj = JSON.parse(obj);
  66.                     return newObj;
  67.                 }catch(e){
  68.                     return false;
  69.                 }
  70.             },
  71.             object : function(obj){
  72.                 try {
  73.                     var newObj = JSON.stringify(obj);
  74.                     return newObj;
  75.                 }catch(e){
  76.                     //will not return false.
  77.                     return false;
  78.                 }
  79.             }
  80.         },
  81.         ajax : function(options){
  82.            
  83.             if(this.typeOf(options) == 'object'){
  84.                
  85.                 if(options.type == null || options.type == ''){
  86.                     return false
  87.                 }else if(options.url == null || options.url == ''){
  88.                     return false;              
  89.                 }else{
  90.                    
  91.                     if(options.async == 'undefined'){
  92.                         options.async = false;
  93.                     }
  94.                    
  95.                     var xhr = new this.testXMLHttpRequest();
  96.                    
  97.                     if(options.type == 'post' || options.type == 'POST'){
  98.                        
  99.                         if(this.typeOf(options.data) == 'object'){
  100.                             console.log(options.data);
  101.                             options.data = this.objString(options.data);
  102.                             console.log(options.data);
  103.                         }
  104.                        
  105.                         xhr.open(options.type, options.url, options.async);
  106.                         xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  107.                         xhr.send(options.data);
  108.                     }else{
  109.                         xhr.open(options.type, options.link, options.async);
  110.                         xhr.send(null);
  111.                     }
  112.                     return xhr.responseText;
  113.                 }
  114.             }else{
  115.                 return false;
  116.             }
  117.         },
  118.         objString: function(obj){
  119.             var string = 'start=true';
  120.             for(var data in obj) {
  121.                 string = string + '&' + data + '=' + obj[data];
  122.             }
  123.             string = string + '&end=true';
  124.             return string;
  125.         }
  126.     };
  127. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement