js function汇总

1。无对话框关闭窗口

Js代码 复制代码
  1. window.opener=null    
  2.  window.open("","_self")    
  3.  window.close();    
   window.opener=null 
    window.open("","_self") 
    window.close();  

2。每次都刷新页面,防止showModalDialog只取缓存数据,

Js代码 复制代码
  1. //网页不保存在缓存中,每次访问都刷新页面。   
  2. <meta http-equiv="cache-control" content="no-cache, must-revalidate">  
//网页不保存在缓存中,每次访问都刷新页面。
<meta http-equiv="cache-control" content="no-cache, must-revalidate">

3.  日期操作

 

Js代码 复制代码
  1. var now = new Date();                    //当前日期      
  2. var nowDayOfWeek = now.getDay();         //今天本周的第几天      
  3. var nowDay = now.getDate();              //当前日      
  4. var nowMonth = now.getMonth();           //当前月      
  5. var nowYear = now.getYear();             //当前年      
  6. nowYear += (nowYear < 2000) ? 1900 : 0;  //      
  7.      
  8. //格式化日期:yyyy-MM-dd      
  9. function formatDate(date) {       
  10.     var myyear = date.getFullYear();      
  11.     var mymonth = date.getMonth()+1;      
  12.     var myweekday = date.getDate();       
  13.           
  14.     if(mymonth < 10){      
  15.         mymonth = "0" + mymonth;      
  16.     }       
  17.     if(myweekday < 10){      
  18.         myweekday = "0" + myweekday;      
  19.     }      
  20.     return (myyear+"-"+mymonth + "-" + myweekday);       
  21. }       
  22.      
  23. //获得某月的天数      
  24. function getMonthDays(myMonth){      
  25.     var monthStartDate = new Date(nowYear, myMonth, 1);       
  26.     var monthEndDate = new Date(nowYear, myMonth + 1, 1);       
  27.     var   days   =   (monthEndDate   -   monthStartDate)/(1000   *   60   *   60   *   24);       
  28.     return   days;       
  29. }      
  30.      
  31. //获得本季度的开始月份      
  32. function getQuarterStartMonth(){      
  33.     var quarterStartMonth = 0;      
  34.     if(nowMonth<3){      
  35.        quarterStartMonth = 0;      
  36.     }      
  37.     if(2<nowMonth && nowMonth<6){      
  38.        quarterStartMonth = 3;      
  39.     }      
  40.     if(5<nowMonth && nowMonth<9){      
  41.        quarterStartMonth = 6;      
  42.     }      
  43.     if(nowMonth>8){      
  44.        quarterStartMonth = 9;      
  45.     }      
  46.     return quarterStartMonth;      
  47. }      
  48.      
  49. //获得本周的开始日期      
  50. function getWeekStartDate() {       
  51.     var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);       
  52.     return formatDate(weekStartDate);      
  53. }       
  54.      
  55. //获得本周的结束日期      
  56. function getWeekEndDate() {       
  57.     var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));       
  58.     return formatDate(weekEndDate);      
  59. }       
  60.      
  61. //获得本月的开始日期      
  62. function getMonthStartDate(){      
  63.     var monthStartDate = new Date(nowYear, nowMonth, 1);       
  64.     return formatDate(monthStartDate);      
  65. }      
  66.      
  67. //获得本月的结束日期      
  68. function getMonthEndDate(){      
  69.     var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));       
  70.     return formatDate(monthEndDate);      
  71. }      
  72.      
  73. //获得本季度的开始日期      
  74. function getQuarterStartDate(){      
  75.           
  76.     var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1);       
  77.     return formatDate(quarterStartDate);      
  78. }      
  79.      
  80. //或的本季度的结束日期      
  81. function getQuarterEndDate(){      
  82.     var quarterEndMonth = getQuarterStartMonth() + 2;      
  83.     var quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth));       
  84.     return formatDate(quarterStartDate);      
  85. }      
  86.      
  87.    
var now = new Date();                    //当前日期   
var nowDayOfWeek = now.getDay();         //今天本周的第几天   
var nowDay = now.getDate();              //当前日   
var nowMonth = now.getMonth();           //当前月   
var nowYear = now.getYear();             //当前年   
nowYear += (nowYear < 2000) ? 1900 : 0;  //   
  
//格式化日期:yyyy-MM-dd   
function formatDate(date) {    
    var myyear = date.getFullYear();   
    var mymonth = date.getMonth()+1;   
    var myweekday = date.getDate();    
       
    if(mymonth < 10){   
        mymonth = "0" + mymonth;   
    }    
    if(myweekday < 10){   
        myweekday = "0" + myweekday;   
    }   
    return (myyear+"-"+mymonth + "-" + myweekday);    
}    
  
//获得某月的天数   
function getMonthDays(myMonth){   
    var monthStartDate = new Date(nowYear, myMonth, 1);    
    var monthEndDate = new Date(nowYear, myMonth + 1, 1);    
    var   days   =   (monthEndDate   -   monthStartDate)/(1000   *   60   *   60   *   24);    
    return   days;    
}   
  
//获得本季度的开始月份   
function getQuarterStartMonth(){   
    var quarterStartMonth = 0;   
    if(nowMonth<3){   
       quarterStartMonth = 0;   
    }   
    if(2<nowMonth && nowMonth<6){   
       quarterStartMonth = 3;   
    }   
    if(5<nowMonth && nowMonth<9){   
       quarterStartMonth = 6;   
    }   
    if(nowMonth>8){   
       quarterStartMonth = 9;   
    }   
    return quarterStartMonth;   
}   
  
//获得本周的开始日期   
function getWeekStartDate() {    
    var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);    
    return formatDate(weekStartDate);   
}    
  
//获得本周的结束日期   
function getWeekEndDate() {    
    var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));    
    return formatDate(weekEndDate);   
}    
  
//获得本月的开始日期   
function getMonthStartDate(){   
    var monthStartDate = new Date(nowYear, nowMonth, 1);    
    return formatDate(monthStartDate);   
}   
  
//获得本月的结束日期   
function getMonthEndDate(){   
    var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));    
    return formatDate(monthEndDate);   
}   
  
//获得本季度的开始日期   
function getQuarterStartDate(){   
       
    var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1);    
    return formatDate(quarterStartDate);   
}   
  
//或的本季度的结束日期   
function getQuarterEndDate(){   
    var quarterEndMonth = getQuarterStartMonth() + 2;   
    var quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth));    
    return formatDate(quarterStartDate);   
}   
  
 

   4. 去掉字符串空格

 

Js代码 复制代码
  1. function String.prototype.trim(){   
  2.       return this.replace(/(^(\s|\u3000)*)|((\s|\u3000)*$)/g, '');   
  3. }  
function String.prototype.trim(){
      return this.replace(/(^(\s|\u3000)*)|((\s|\u3000)*$)/g, '');
}

    5.禁止反复提交

   

Js代码 复制代码
  1. //首先在form中加入   
  2. οnsubmit="return checkSubmit();"  
  3. //例如:   
  4. <html:form enctype="multipart/form-data" action="<%=url %>"    
  5. method="post" οnsubmit="return checkSubmit();">     
  6.   
  7. //再如以下代码:   
  8. var checkSubmitFlg = false;    
  9. function checkSubmit() {    
  10.   if (checkSubmitFlg == true) {    
  11.       return false;    
  12.   }    
  13.   checkSubmitFlg = true;    
  14.   return true;    
  15. }    
  16. document.ondblclick = function docondblclick() {    
  17.   window.event.returnValue = false;    
  18. }    
  19. document.onclick = function doconclick() {    
  20. if (checkSubmitFlg) {    
  21.           window.event.returnValue = false;    
  22.      }    
  23. }  
//首先在form中加入
οnsubmit="return checkSubmit();"
//例如:
<html:form enctype="multipart/form-data" action="<%=url %>" 
method="post" οnsubmit="return checkSubmit();">  

//再如以下代码:
var checkSubmitFlg = false; 
function checkSubmit() { 
  if (checkSubmitFlg == true) { 
      return false; 
  } 
  checkSubmitFlg = true; 
  return true; 
} 
document.ondblclick = function docondblclick() { 
  window.event.returnValue = false; 
} 
document.onclick = function doconclick() { 
if (checkSubmitFlg) { 
     	  window.event.returnValue = false; 
     } 
}

 6.失去焦点调用方法

 

Js代码 复制代码
  1. var fstationname = document.getElementById("fsApprovebookDto.fstationname");   
  2. fstationname.οnblur=getStationnameCount;   
  3. //getStationnameCount function名称  
var fstationname = document.getElementById("fsApprovebookDto.fstationname");
fstationname.οnblur=getStationnameCount;
//getStationnameCount function名称

 7.iframe自适应窗体大小

 

Java代码 复制代码
  1. function sizeOfwindow(){   
  2.         var report = document.getElementById("report");//report为iframe的id   
  3.          report.height= document.body.offsetHeight - 130;   
  4.          report.width =  document.body.offsetWidth - 60;   
  5. }   
  6. window.onresize = function(){   
  7.          sizeOfwindow();   
  8. }     
function sizeOfwindow(){
        var report = document.getElementById("report");//report为iframe的id
         report.height= document.body.offsetHeight - 130;
         report.width =  document.body.offsetWidth - 60;
}
window.onresize = function(){
         sizeOfwindow();
}	

    8.button置灰:

 

Java代码 复制代码
  1. document.getElementById("submits").disabled="true";//submits is button's id  
document.getElementById("submits").disabled="true";//submits is button's id

   9.Div隐藏 

 

Js代码 复制代码
  1. var outstore = document.getElementById("outstore");   
  2. outstore.style.display = "block";  
var outstore = document.getElementById("outstore");
outstore.style.display = "block";

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值