常用javascript方法

//字符串去重複
        function StrDistinct(swd) {
            Array.prototype.unique = function() {
                var a = {}; for (var i = 0; i < this.length; i++) {
                    if (typeof a[this[i]] == "undefined")
                        a[this[i]] = 1;
                }
                this.length = 0;
                for (var i in a)
                    this[this.length] = i;
                return this;
            }
            var swdArray = swd.split(",")
            var hahaArray = swdArray.unique();
            var newStr = hahaArray.join(",");
            return newStr;
        }

//在屏幕中央彈出新窗口
function openNewWindow(width,height,url){

    var startW = (screen.availWidth - width)/2;
    var startH = (screen.availHeight - height)/2;
    window.open(url,"_blank","left="+startW+",top="+startH+",height="+height+",width="+width+",status=no,resizable=1,scrollbars=yes,toolbar=no,menubar=no,location=no");
}
//在屏幕中央彈出打開Word的新窗口 added by zengbo 2007-10-24
function openWordWindow(url){
    var width=800;
    var height=600;
    var startW = (screen.availWidth - width)/2;
    var startH = (screen.availHeight - height)/2;
    window.open(url,"_blank","left="+startW+",top="+startH+",height="+height+",width="+width+",status=no,resizable=1,scrollbars=yes,toolbar=no,menubar=yes,location=no");
}

//在屏幕中央彈出新窗口
function openNewWindowByMenu(width,height,url){
    var startW = (screen.availWidth - width)/2;
    var startH = (screen.availHeight - height)/2;
    window.open(url,"_blank","left="+startW+",top="+startH+",height="+height+",width="+width+",status=yes,resizable=1,scrollbars=yes,toolbar=yes,menubar=yes,location=no");
}

//彈出全屏窗口
function openNewWindowByMenu(url){
    window.open(url,"_blank","left=0,top=0,height="+screen.availHeight+",width="+screen.availWidth+",status=yes,resizable=1,scrollbars=yes,toolbar=yes,menubar=yes,location=no");
}

//在屏幕中央彈出模態窗口
function openModelWindow(width,height,url){
//    var d = new Date;
//    if(url.lastIndexOf("?")==-1){
//        url = url+"?currTime="+d.getTime();
//    }else{
//        url = url+"&currTime="+d.getTime();
//    }
    window.showModalDialog(url, window, "dialogHeight:" + height + "px;dialogWidth:" + width + "px;center:Yes;Help:No;Resizable:No;Scroll:yes;Status:no;");   
}

//在屏幕中央彈出公共的查詢模態窗口
function openCommonQueryModelWindow(url){
    openModelWindow(520,320,url);
}

//全選
function GVSelectAll(){
    var len=document.forms[0].elements.length;
    var i;
    for (i=0;i<len;i++){
        if (document.forms[0].elements[i].type=="checkbox" && document.forms[0].elements[i].name.indexOf("GridViewID")>-1){
             document.forms[0].elements[i].checked=true;
        }
    }
}
//全選
function GVSelectAll(check){
    var len=document.forms[0].elements.length;
    var i;
    for (i=0;i<len;i++){
        if (document.forms[0].elements[i].type=="checkbox" && document.forms[0].elements[i].name.indexOf("GridViewID")>-1){
             document.forms[0].elements[i].checked=check;
        }
    }
}
//全選
function GVSelectAll1(check) {
    var len = document.forms[0].elements.length;
    var i;
    for (i = 0; i < len; i++) {
        if (document.forms[0].elements[i].type == "checkbox" && document.forms[0].elements[i].name.indexOf("gvcheck") > -1) {
            document.forms[0].elements[i].checked = check;
        }
    }
}
//反選
function GVSelectRever(){
    var len=document.forms[0].elements.length;
    var i;
    for (i=0;i<len;i++){
        if (document.forms[0].elements[i].type=="checkbox" && document.forms[0].elements[i].name.indexOf("GridViewID")>-1){
                if(document.forms[0].elements[i].checked){
                    document.forms[0].elements[i].checked = false;
                }else{
                    document.forms[0].elements[i].checked=true;                               
                }
        }
    }
}

//取消選擇
function GVSelectCancel(){
    var len=document.forms[0].elements.length;
    var i;
    for (i=0;i<len;i++){
        if (document.forms[0].elements[i].type=="checkbox" && document.forms[0].elements[i].name.indexOf("GridViewID")>-1){
            document.forms[0].elements[i].checked = false;
        }
    }
}

//GirdView選擇一條記錄進行操作前的確認
function GVSelectOneConfirm(){
    var selectCount = 0;
    for(i=0; i<document.forms[0].elements.length;i++){
        if(document.forms[0].elements[i].type=="checkbox" && document.forms[0].elements[i].name.indexOf("GridViewID")>-1 && document.forms[0].elements[i].checked) selectCount++;
    }
    if(selectCount<1){
        alert("請勾選一條數據,然後再點擊按鈕進行操作!");
        return false;
    }else if(selectCount>1){
        alert("只能同時操作一條數據,請檢查你是否勾選了多條數據!");
        return false;
    }else{
        return true;
    }
    return false;
}

//GridView選擇一條記錄進行操作前的確認,確認提示信息由用戶自定義
function GVSelectOneConfirmByParm(str){
    var selectCount = 0;
    for(i=0; i<document.forms[0].elements.length;i++){
        if(document.forms[0].elements[i].type=="checkbox" && document.forms[0].elements[i].name.indexOf("GridViewID")>-1 && document.forms[0].elements[i].checked) selectCount++;
    }
    if(selectCount<1){
        alert("請勾選一條數據,然後再點擊按鈕進行操作!");
        return false;
    }else if(selectCount>1){
        alert("只能同時操作一條數據,請檢查你是否勾選了多條數據!");
        return false;
    }else{
        return confirm(str);
    }
    return false;
}

//GridView選擇一條或多條記錄進行操作前,判斷是否選擇了記錄
function GVSelectOneOrMore(){
    var selectCount = 0;
   
    for(i=0; i<document.forms[0].elements.length;i++){
        if(document.forms[0].elements[i].type=="checkbox" && document.forms[0].elements[i].name.indexOf("GridViewID")>-1 && document.forms[0].elements[i].checked) selectCount++;
    }
    if(selectCount<1){
        alert("請至少勾選一條數據,然後再點擊按鈕進行操作!");
        return false;
    }
    return true;
}

//GridView最少必須保留一項
function GVUnSelectOne(){
    var selectCount = 0;
    var rowCount = 0;
    for(i=0; i<document.forms[0].elements.length;i++){
       if(document.forms[0].elements[i].type=="checkbox" && document.forms[0].elements[i].name.indexOf("GridViewID")>-1 )
       {
             rowCount++;
             if(document.forms[0].elements[i].checked)
             selectCount++;
       }
    }
     if(selectCount<1){
        alert("請至少勾選一條數據,然後再點擊按鈕進行操作!");
        return false;
    }
    if(selectCount==rowCount)
    {
        alert("最少必須保留一條數據!");
        return false;
    }
    return true;
}

//GridView選擇一條或多條記錄進行操作前的確認
function GVSelectOneOrMoreConfirm(){
    var selectCount = 0;
    for(i=0; i<document.forms[0].elements.length;i++){
        if(document.forms[0].elements[i].type=="checkbox" && document.forms[0].elements[i].name.indexOf("GridViewID")>-1 && document.forms[0].elements[i].checked) selectCount++;
    }
    if(selectCount<1){
        alert("請至少勾選一條數據,然後再點擊按鈕進行操作!");
        return false;
    }else{
        return confirm("刪除後數據將不能恢復,你確定要刪除數據嗎?");
    }
    return false;
}

//GridView選擇一條或多條記錄進行操作前的確認-合同過賬
function GVSelectOneOrMoreConfirm_Posting(){
    var selectCount = 0;
    for(i=0; i<document.forms[0].elements.length;i++){
        if(document.forms[0].elements[i].type=="checkbox" && document.forms[0].elements[i].name.indexOf("GridViewID")>-1 && document.forms[0].elements[i].checked) selectCount++;
    }
    if(selectCount<1){
        alert("請至少勾選一條數據,然後再點擊按鈕進行操作!");
        return false;
    }else{
        return confirm("確定要對所選合同過賬嗎?");
    }
    return false;
}

//GridView選擇一條或多條記錄進行操作前的確認-企業分配
function GVSelectOneOrMoreConfirm_Assign() {
    var selectCount = 0;
    for (i = 0; i < document.forms[0].elements.length; i++) {
        if (document.forms[0].elements[i].type == "checkbox" && document.forms[0].elements[i].name.indexOf("GridViewID") > -1 && document.forms[0].elements[i].checked) selectCount++;
    }
    if (selectCount < 1) {
        alert("請至少勾選一條數據,然後再點擊按鈕進行操作!");
        return false;
    }
    return false;
}

//GridView選擇一條或多條記錄進行操作前的確認
function GVSelectOneOrMore(){
    var selectCount = 0;
    for(i=0; i<document.forms[0].elements.length;i++){
        if(document.forms[0].elements[i].type=="checkbox" && document.forms[0].elements[i].name.indexOf("GridViewID")>-1 && document.forms[0].elements[i].checked) selectCount++;
    }
    if(selectCount<1){
        alert("請至少勾選一條數據,然後再點擊按鈕進行操作!");
        return false;
    }else{
        return true;
    }
    return false;
}

//GridView選擇一條或多條記錄進行操作前的確認,確認提示信息由用戶自定義
function GVSelectOneOrMoreConfirmByParm(str){
    var selectCount = 0;
    for(i=0; i<document.forms[0].elements.length;i++){
        if(document.forms[0].elements[i].type=="checkbox" && document.forms[0].elements[i].name.indexOf("GridViewID")>-1 && document.forms[0].elements[i].checked) selectCount++;
    }
    if(selectCount<1){
        alert("請至少勾選一條數據,然後再點擊按鈕進行操作!");
        return false;
    }else{
        return confirm(str);
    }
    return false;
}


function confirmMsg (){
    j = 0;
    for (i=0; i<document.forms[0].length; i++){
        if (document.forms[0].elements[i].name.indexOf("checkbox") !=-1){
            if(document.forms[0].elements[i].checked){
                j++;
                return confirm ('刪除後數據將不能恢復,你確定要刪除數據嗎?')
            }
        }
    }
    if (j == 0){
       alert ("請至少勾選一條數據,然後再點擊按鈕進行操作!");
       return false;
    }
}
//去除兩頭空格
function trim(str)
 { 
 var str1 = str.replace(/^/s+|/s+$/g, "");
 return str1;
}
//給String類加上trim()方法,去掉前後空格
String.prototype.trim = function()
{
    return this.replace(/(^/s*)|(/s*$)/g, "");
}

//getPageSize函數返回一個數組,前兩個是整個頁面的寬度和高度,後兩個是頁面窗口的寬度和高度
function getPageSize(varWindow)
{
    var xScroll, yScroll;
    if (varWindow.innerHeight && varWindow.scrollMaxY)
    {
        xScroll = varWindow.document.body.scrollWidth;
        yScroll = varWindow.innerHeight + varWindow.scrollMaxY;
    }
    else if (varWindow.document.body.scrollHeight > varWindow.document.body.offsetHeight)
    {
        xScroll = varWindow.document.body.scrollWidth;
        yScroll = varWindow.document.body.scrollHeight;
    }
    else
    {
        xScroll = varWindow.document.body.offsetWidth;
        yScroll = varWindow.document.body.offsetHeight;
    }
    var windowWidth, windowHeight;
    if (varWindow.innerHeight)
    {
        windowWidth = varWindow.innerWidth;
        windowHeight = varWindow.innerHeight;
    }
    else if (varWindow.document.documentElement && varWindow.document.documentElement.clientHeight)
    {
        windowWidth = varWindow.document.documentElement.clientWidth;
        windowHeight = varWindow.document.documentElement.clientHeight;
    }
    else if (varWindow.document.body)
    {
        windowWidth = varWindow.document.body.clientWidth;
        windowHeight = varWindow.document.body.clientHeight;
    }
    if (yScroll < windowHeight)
    {
        pageHeight = windowHeight;
    }
    else
    {
        pageHeight = yScroll;
    }
    if (xScroll < windowWidth)
    {
        pageWidth = windowWidth;
    }
    else
    {
        pageWidth = xScroll;
    }
    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
    return arrayPageSize;
}

//getScrollTop函數獲得當前頁面的滾動條縱座標位置。在給頁面添加了
//<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
//後,document.body.scrollTop永遠等於0。此時可以用這個方法來獲得scrollTop。
function getScrollTop()
{      
    if (typeof window.pageYOffset != 'undefined')
    {
       return window.pageYOffset;
    }
    else if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat')
    {
       return document.documentElement.scrollTop;
    }
    else if (typeof document.body != 'undefined')
    {
       return document.body.scrollTop;
    }
}

//設置按鈕是否顯示,visible爲true時顯示,否則不顯示,id爲要設置的按鈕id
function setButtonVisible(id,visible)
{
    var space = document.getElementById(id+"_Space");
    if(space!=null){
        space.style.display = visible==true?"":"none";
    }
    var button = document.getElementById(id);
    if(button!=null){
        button.style.display = visible==true?"":"none";
    }
}

// 自適應iframe調度
function autoresetIframeHeight() {
    if (parent.document.all["IframeContent"] != null) {
        //parent.document.all["IframeContent"].style.width = document.body.scrollWidth;
        parent.document.all["IframeContent"].style.height = document.body.scrollHeight + 20;
    }
    if (parent.document.all["IframeContent"] != null) {
        //parent.document.all["IframeContent"].style.width = document.body.scrollWidth;
        parent.document.all["IframeContent"].style.height = document.body.scrollHeight + 20;
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章