X龙@China .Net 'blog

需要的不仅仅是工作,而是通过努力得来的美好将来。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

JS 常用函数

Posted on 2011-03-08 13:23  X龙  阅读(216)  评论(0编辑  收藏  举报
// 格式化字符串:示例:"test{0}".format("test2") 
String.prototype.format = function()
 {
     var args = arguments;
 
     return this.replace(/\{(\d+)\}/g,               
      function(m,i){
         return args[i];
     });
 }
 
// 获取指定长度字符串
//    source长度超过location指定长度,在指定长度后面连接...
function GetSubstring(source, location)
{
    if (source)
    {
        if (source.length > location)
        {
            return source.substring(0, location) + "...";
        }

        return source;
    }
}
 
// 读取查询字符串参数
Request = {
            QueryString: function(item) {
                var svalue = location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)", "i"));
                return svalue ? svalue[1] : svalue;
            }
          }         
 
//=========打开弹出窗口 ============== 
// 参数分别为:文件URL, 宽度, 高度, 是否有滚动条(还可以改变大写) 
function OpenWindow(szUrl, iWidth, iHeight, bScroll) { 
 if (iWidth == null) iWidth = 0.95; 
 if (iHeight == null) iHeight = 0.7; 
 var width, height, left, top; 
 if (iWidth > 1) { 
 width = iWidth; 
 height = iHeight; 
 } 
 else { 
 width = screen.availWidth * iWidth; 
 height = screen.availHeight * iHeight; 
 } 
 var feature = ""; 
 //if (bScroll == 1 ) 
 //{ 
 feature = "scrollbars=yes,status=yes,resizable=yes,toolbar = yes,"; 
 //} 
 left = parseInt(screen.availWidth - width) / 2; 
 top = parseInt(screen.availHeight - height) / 2; 
 feature += "width=" + width + ",height=" + height + ",left=" + left + ",top=" + top; 
 var win = window.open(szUrl, "_blank", feature); 
 win.focus(); 
} 
 
 
//=========显示模式对话框 ============== 
// 参数分别为:文件URL, 宽度, 高度, 是否有滚动条(还可以改变大写) 
// 此函数解决了模式对话框中打开的窗口提交时弹出新页面的问题,使用到了ModelDialogFrame.htm文件 
function OpenModalDialog(szUrl, iWidth, iHeight, bScroll, szTitle) { 
 if (iWidth == null) iWidth = 0.95; 
 if (iHeight == null) iHeight = 0.7; 
 var width, height; 
 if (iWidth > 1) { 
 width = iWidth; 
 height = iHeight; 
 } 
 else { 
 width = screen.availWidth * iWidth; 
 height = screen.availHeight * iHeight; 
 } 
 var feature = "dialogHeight: " + height + "px; dialogWidth: " + width + "px; dialogTop: px; dialogLeft: px; edge: Sunken; center: Yes; help: No; scroll:auto;"; 
 if (bScroll == 1) { 
 feature += "resizable: Yes; status: Yes;"; 
 } 
 else { 
 feature += "resizable: No; status: No;"; 
 } 
 var r = Math.random() + Math.random(); 
 var frameUrl = "/ModalDialogFrame.htm?r=" + r; 
 if (szUrl.indexOf("?") < 0) { 
 szUrl += "?r=" + r; 
 } 
 else { 
 szUrl += "&r=" + r; 
 } 
 var args = new Array(szUrl, szTitle); 
 return window.showModalDialog(frameUrl, args, feature); 
} 
OpenModalDialog使用的ModalDialogFrame.htm下载
点击这里给我发消息http://wp.qq.com/index.html