Go to Google Groups Home    学习日志
常用JS函数

Chen Liqiang <chen...@gmail.com>

/**
 * 去除空格
 */
String.prototype.trim = function()
{
        return this.replace(/(^[\s| ]*)|([\s| ]*$)/g, "");

}

/**
 * 去除左空格
 */
String.prototype.ltrim = function()
{
        return this.replace(/(^[\s| ]*)/g, "");
}

/**
 * 去除右空格
 */
String.prototype.ltrim = function()
{
        return this.replace(/([\s| ]*$)/g, "");

}

/**
 * vbscript 的left函数,取字符串最左边len个字符
 */
function left(str,len) {
        return str.substr(0,len);
}

/**
 * vbscript 的right函数,取字符串最右边len个字符
 */
function right(str,len) {
        if (str.length-len>=0 && str.length>=0 && str.length-len<=str.length)
{
                return str.substring(str.length-len,str.length)
        } else {
                return null
        };
}

/**
 * 简化getElementById
 */
function $(s)
{
        return document.getElementById(s);

}

/**
 * 创建新html元件
 */
function $c(s)
{
        return document.createElement(s);

}

/**
 * 判断html元件是否存在
 */
function exist(s)
{
        return $(s)!=null;

}

/**
 * 输出html内容
 */
function dw(s)
{
        document.write(s);

}

/**
 * 判断一个变量是否为空
 */
function isNull(_sVal)
{
        return (_sVal == "" || _sVal == null || _sVal == "undefined");

}

/**
 * 删除节点
 */
function removeNode(s)
{
        if(exist(s))
        {
                $(s).innerHTML = '';
                $(s).removeNode?$(s).removeNode():$(s).parentNode.removeChild($(s));
        }

}

/**
 * 刷新页面
 */
function refreshPage() {
        window.location.reload();

}

/**
 * 字符串长度
 */
function strlen(key){
        var l=escape(key),len;
        len=l.length-(l.length-l.replace(/\%u/g,"u").length)*4;
        l=l.replace(/\%u/g,"uu");
        len=len-(l.length-l.replace(/\%/g,"").length)*2;
        return len;
}

/**
 * 取URL参数
 */
function GetUrlParam( paramName )
{
        var oRegex = new RegExp( '[\?&]' + paramName + '=([^&]+)', 'i' ) ;
        var oMatch;
        try {
                oMatch = oRegex.exec( window.top.location.search ) ;
        } catch (e) {
            oMatch = oRegex.exec( location.search ) ;
        }

        if ( oMatch && oMatch.length > 1 )
                return unescape( oMatch[1] ) ;
        else
                return '' ;

}