zoukankan      html  css  js  c++  java
  • Javascript Common Tools Class

    /*
    Javascript Common Tools Class 
    Email : Daniel.Chow.Tech@Hotmail.Com
    Blog : http://www.cnblogs.com/DanielChow
    */
    // 2010-2-20 update
    var Js = new Object;
    Js.IsDebug 
    = true;
    Js.Domain 
    = "127.0.0.1";
    Js.NetUrl 
    = "http://127.0.0.1";
    Js.Locale 
    = "/";
    Js.CopyRight 
    = "(C) Your.COM";
    Js.Event 
    = new Object;
    Js.Event.add 
    = function(o, t, f) {
        
    if (o.addEventListener) {
            o.addEventListener(t, f, 
    false);
        } 
    else if (o.attachEvent) {
            o.attachEvent(
    "on" + t, f);
        } 
    else {
            o[
    "on" + t] = f;
        }
    };
    Js.Event.remove 
    = function(o, t, f) {
        
    if (o.removeEventListener) {
            o.removeEventListener(t, f, 
    false);
        } 
    else if (o.detachEvent) {
            o.detachEvent(
    "on" + t, f);
        } 
    else {
            o[
    "on" + t] = null;
        }
    };
    /*compatible in IE6+,ff3.0,chrome,oprea*/
    Js.floatElement 
    = function(Id, Position) {   
        _bodyfrm 
    = (document.compatMode.toLowerCase() == "css1compat"? document.documentElement : document.body;
        _adst 
    = document.getElementById(Id).style;_adst.position = "absolute"; _intWidth = _adst.width.replace("px"""); _intHeight = _adst.height.replace("px""");
        _move(Position);window.onscroll 
    = window.onresize = function() {_move(Position);};
        
    function _move(Position) {
            
    switch (Position) {
                
    case "rightbottom": _adst.left = (_bodyfrm.scrollLeft + _bodyfrm.clientWidth - _intWidth) + "px";_adst.top = (_bodyfrm.clientHeight + _bodyfrm.scrollTop - _intHeight) + "px";break;
                
    case "leftbottom": _adst.left = "0px";_adst.top = (_bodyfrm.clientHeight + _bodyfrm.scrollTop - _intHeight) + "px";break;
                
    case "righttop": _adst.left = (_bodyfrm.scrollLeft + _bodyfrm.clientWidth - _intWidth) + "px";_adst.top = _bodyfrm.scrollTop + "px"break;
                
    case "lefttop": _adst.left = "0px";  _adst.top = _bodyfrm.scrollTop + "px"break;
            };
        };
    };
    Js.Guid 
    = function() {
        
    var guid = "";
        
    for (var i = 1; i <= 32; i++) {
            
    var n = Math.floor(Math.random() * 16.0).toString(16);
            guid 
    += n;
            
    if ((i == 8|| (i == 12|| (i == 16|| (i == 20))
                guid 
    += "-";
        }
        
    return guid;
    };
    Js.SuggestStr 
    = function(len, IsAC) {
        
    //include additional characters
        var AC = 'abcdefhijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWYXZ~!@$^*+-|/'
        
    var noAC = 'abcdefhijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWYXZ'
        
    function returnStr(list, len) {
           
    var re = '';
            
    for (i = 0; i < len; i++) {
                re 
    += list.charAt(Math.floor(Math.random() * list.length))
            }
            
    return re;
        }
        
    return IsAC ? returnStr(AC, len) : returnStr(noAC, len);
    };
    Js.Str2XML 
    = function(str) {
        
    if (window.ActiveXObject) {
            xmlDoc 
    = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async 
    = "false";
            xmlDoc.loadXML(str);
            
    return xmlDoc;
        } 
    else {
            parser 
    = new DOMParser();
            xmlDoc 
    = parser.parseFromString(str, "text/xml");
            
    return xmlDoc;
        }
    };
    Js.XML2Str 
    = function(xmlObject) {
        
    if (window.ActiveXObject) {
            
    return xmlObject.xml;
        } 
    else {
            
    return (new XMLSerializer()).serializeToString(xmlObject);
        }
    };
    Js.Str2Json 
    = function(str) {
       
    return eval('(' + str + ')');
    }
    Js.ErrorHandler 
    = function(msg, url, l) {
        
    //if (Js.IsDebug) {
        //    // for debug
        //    var txt = "";
        //    txt = "本页中存在错误。\n\n"
        //    txt += "错误:" + msg + "\n"
        //    txt += "URL: " + url + "\n"
        //    txt += "行:" + l + "\n\n"
        //    txt += "点击“确定”继续。\n\n"
        //    alert(txt);
        //}
        return false;
        
    // false , page response errors directly
        // true , var script above to handle errors
        // or you can save errors to server (to create logs file)
    };

    Js.Url 
    = {
        encode: 
    function(string) {
            
    return escape(this._utf8_encode(string));
        },
        decode: 
    function(string) {
            
    return this._utf8_decode(unescape(string));
        },
        request:
    function(string)
        {
            
    var url = location.href; var paraObj = {}; var paraString = url.substring(url.indexOf("?"+ 1, url.length).split("&");        
            
    for (i = 0; j = paraString[i]; i++) { paraObj[j.substring(0, j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("="+ 1, j.length);}
            
    var returnValue = paraObj[string.toLowerCase()];
            
    if (typeof (returnValue) == "undefined") { return ""; } else { return returnValue; }
        },
        
    // private method
        _utf8_encode: function(string) {
            string 
    = string.replace(/\r\n/g, "\n");
            
    var utftext = "";
            
    for (var n = 0; n < string.length; n++) {
                
    var c = string.charCodeAt(n);
                
    if (c < 128) {
                    utftext 
    += String.fromCharCode(c);
                }
                
    else if ((c > 127&& (c < 2048)) {
                    utftext 
    += String.fromCharCode((c >> 6| 192);
                    utftext 
    += String.fromCharCode((c & 63| 128);
                }
                
    else {
                    utftext 
    += String.fromCharCode((c >> 12| 224);
                    utftext 
    += String.fromCharCode(((c >> 6& 63| 128);
                    utftext 
    += String.fromCharCode((c & 63| 128);
                }
            }
            
    return utftext;
        },
        _utf8_decode: 
    function(utftext) {
            
    var string = "";
            
    var i = 0;
            
    var c = c1 = c2 = 0;
            
    while (i < utftext.length) {
                c 
    = utftext.charCodeAt(i);
                
    if (c < 128) {
                    string 
    += String.fromCharCode(c);
                    i
    ++;
                }
                
    else if ((c > 191&& (c < 224)) {
                    c2 
    = utftext.charCodeAt(i + 1);
                    string 
    += String.fromCharCode(((c & 31<< 6| (c2 & 63));
                    i 
    += 2;
                }
                
    else {
                    c2 
    = utftext.charCodeAt(i + 1);
                    c3 
    = utftext.charCodeAt(i + 2);
                    string 
    += String.fromCharCode(((c & 15<< 12| ((c2 & 63<< 6| (c3 & 63));
                    i 
    += 3;
                }
            }
            
    return string;
        }
    }

    --------------------------------------------------------------------------
    Daniel Chow's Blog - 不管你在哪里,都要有一颗创业的心!
    http://www.cnblogs.com/DanielChow/
  • 相关阅读:
    log4j使用教程
    (POI)Excel格式转Html格式
    log4j2使用教程
    Spring AOP 面向切面编程入门
    C# 标准事件模式
    1Angular的MVC和作用域
    3Angular的模块化
    2Angular的双向数据绑定(MVVM)
    5手动初始化Angular的模块与控制器
    python读取 UCS2 little endian(utf16le) 格式的文件
  • 原文地址:https://www.cnblogs.com/DanielChow/p/1638503.html
Copyright © 2011-2022 走看看