百度首页 | 百度空间
 
查看文章
 
lite tools
2008-07-01 00:34
(function(bootstrap){
   if( window.$__loaded ) return ;
   window.$__loaded   = true;
   var temp = document.createElement('div');
   (function(){
       try{
           temp.doScroll('left');
           document.body.appendChild(temp);
           temp.innerHTML   = 'temp';
           document.body.removeChild(temp);
       }catch(e){
           return setTimeout(arguments.callee, 50);
       }
       bootstrap();
       try{setTimeout(window.$domready, 1);}catch(e){}
   })();
})(function(){
   function $impl( obj ){
       if(this instanceof Array){
           for (var i = 0, l = this.length; i < l; i++){
               arguments.callee.call(this[i], obj);
           }
           return ;
       }else{
           for(var key in obj ){
               if( !this.prototype[key] ){
                   this.prototype[key]   = obj[key];
               }
           }
       }
   }

   if( !window.$ ){
       window.$   = function(id){
           return document.getElementById(id);
       }
   }

   $impl.call(Array, {
       forEach: function(fn, bind){
           for (var i = 0, l = this.length; i < l; i++) fn.call(bind, this[i], i, this);
       },
       filter: function(fn, bind){
           var results = [];
           for (var i = 0, l = this.length; i < l; i++){
               if (fn.call(bind, this[i], i, this)) results.push(this[i]);
           }
           return results;
       },
       map: function(fn, bind){
           var results = [];
           for (var i = 0, l = this.length; i < l; i++) results[i] = fn.call(bind, this[i], i, this);
           return results;
       }
   });

   Array.prototype.each   = Array.prototype.forEach;

   $impl.call(String, {
       test: function(regex, params){
           return ((typeof regex == 'string') ? new RegExp(regex, params) : regex).test(this);
       },
       trim: function(){
           return this.replace(/^\s+|\s+$/g, '');
       }
   });

   // JSON
   if( !window.JSON ){
       window.JSON = (function(){
           var useHasOwn = !!{}.hasOwnProperty;

           var pad = function(n) {
               return n < 10 ? "0" + n : n;
           };

           var m = {
               "\b": '\\b',
               "\t": '\\t',
               "\n": '\\n',
               "\f": '\\f',
               "\r": '\\r',
               '"' : '\\"',
               "\\": '\\\\'
           };

           var encodeString = function(s){
               if (/["\\\x00-\x1f]/.test(s)) {
                   return '"' + s.replace(/([\x00-\x1f\\"])/g, function(a, b) {
                       var c = m[b];
                       if(c){
                           return c;
                       }
                       c = b.charCodeAt();
                       return "\\u00" +
                           Math.floor(c / 16).toString(16) +
                           (c % 16).toString(16);
                   }) + '"';
               }
               return '"' + s + '"';
           };

           var encodeArray = function(o){
               var a = ["["], b, i, l = o.length, v;
                   for (i = 0; i < l; i += 1) {
                       v = o[i];
                       switch (typeof v) {
                           case "undefined":
                           case "function":
                           case "unknown":
                               break;
                           default:
                               if (b) {
                                   a.push(',');
                               }
                               a.push(v === null ? "null" : encode(v));
                               b = true;
                       }
                   }
                   a.push("]");
                   return a.join("");
           };


           var encode = function(o){
               if(typeof o == "undefined" || o === null){
                   return "null";
               }else if(o instanceof Array){
                   return encodeArray(o);
               }else if(typeof o == "string"){
                   return encodeString(o);
               }else if(typeof o == "number"){
                   return isFinite(o) ? String(o) : "null";
               }else if(typeof o == "boolean"){
                   return String(o);
               }else {
                   var a = ["{"], b, i, v;
                   for (i in o) {
                       if(!useHasOwn || o.hasOwnProperty(i)) {
                           v = o[i];
                           switch (typeof v) {
                           case "undefined":
                           case "function":
                           case "unknown":
                               break;
                           default:
                               if(b){
                                   a.push(',');
                               }
                               a.push(this.encode(i), ":",
                                       v === null ? "null" : this.encode(v));
                               b = true;
                           }
                       }
                   }
                   a.push("}");
                   return a.join("");
               }
           };

           var decode = function(json){
               return eval("(" + json + ')');
           };
           return {encode: encode , decode : decode}
       })();
   }
});

类别:Web | 添加到搜藏 | 浏览() | 评论 (2)
 
最近读者:
 
网友评论:
2
2008-07-01 00:36
发表文章的字数太少了,要不再发个css selector了。
 
3
2008-07-01 00:48
var Engine = 'unknown';
if( window.opera ){
Engine = 'presto';
}else if(window.ActiveXObject){
Engine = 'trident';
}else if (!navigator.taintEnabled){
Engine = 'webkit';
}else if (document.getBoxObjectFor != null) {
Engine = 'gecko';
}

switch ( Engine ){

case 'webkit':
(function(){
(['loaded', 'complete'].contains(document.readyState)) ? domready() : setTimeout(arguments.callee, 50);
})();
break;

case 'trident':
var temp = document.createElement('div');
(function(){
try{
temp.doScroll('left');
document.body.appendChild(temp);
temp.innerHTML = 'temp';
document.body.removeChild(temp);
}catch(e){
return setTimeout(arguments.callee, 50);
}
domready();
})();
break;

default:
window.addEventListener('load', domready, false);
document.addEventListener('DOMContentLoaded', domready, false);
}
 
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码:
 

     

©2008 Baidu