一个sohoto广告弹出页提取时间的Js修改过程

以前页面就有浏览器兼容问题 只能ie浏览 今天有空看了一下代码 确实比较糟糕 代码如下:

 1 <input type="hidden" name="checkInDate" id="checkInDate" />
 2 <SCRIPT LANGUAGE="JavaScript">
 3   var nowtoday = new Date();
 4   var showMonth = nowtoday.getMonth() + 1;
 5   var showDate = "";
 6   if (nowtoday.getMonth() < 9)
 7     showMonth = "0" + showMonth;
 8   if (nowtoday.getDate() < 10)
 9     showDate = "0" + nowtoday.getDate();
10   else
11     showDate = nowtoday.getDate();
12   document.getElementById("checkInDate").value = nowtoday.getYear() + "-"
13       + showMonth + "-" + showDate;
14 </SCRIPT>
15 
16 <input type="hidden" name="checkOutDate" id="checkOutDate" />
17 <SCRIPT LANGUAGE="JavaScript">
18   var nowtoday = new Date();
19   var nextday = new Date(nowtoday.getTime() + 24 * 3600 * 1000);
20   var showMonth = nextday.getMonth() + 1;
21   var showDate = "";
22   if (nextday.getMonth() < 9)
23     showMonth = "0" + showMonth;
24   if (nextday.getDate() < 10)
25     showDate = "0" + nextday.getDate();
26   else
27     showDate = nextday.getDate();
28   document.getElementById("checkOutDate").value = nextday.getYear() + "-"
29       + showMonth + "-" + showDate;
30 </SCRIPT>
31 
32 <script type="text/javascript">
33 function goHotelInformation(hotelId){
34   var checkInDate=document.getElementById("checkInDate").value;
35   var checkOutDate=document.getElementById("checkOutDate").value;
36   var path=location.pathname;
37     var idx=path.indexOf("/",1);
38     var ctx=path.substring(0,idx);
39     window.open(ctx+"/htl/order/HotelInfomation.do?hotelId="+hotelId+"&checkInDate="+checkInDate+"&checkOutDate="+checkOutDate+"&c=1&m=1&L=0");
40 }
41 </script>

我们看到有3个script标签 还有重复的全局变量 

大致的功能就是取到今天和明天的日期 存储到两个input里 然后在根据两个日期查询酒店

功能很简单 但是过程很复杂 在这里做一些修改:

1.浏览器兼容问题是getYear()方法造成的 这里用getFullYear()

2.没必要把取到的日期存进input在调用input的value 这里写个方法直接调用日期

一个script 两个方法搞定

getDay().nowtoday和getDay().nextday还可以供别的页面使用 代替了更多的全局变量

 1 <script type="text/javascript">
 2 function getDay(){
 3   var nowtoday = new Date(),  //今天
 4       nextday = new Date(nowtoday.getTime() + 24 * 3600 * 1000);  //明天
 5 
 6   //月份+1
 7   var showMonth = nowtoday.getMonth() + 1,
 8       nextShowMonth = nextday.getMonth() + 1;
 9 
10   //日期
11   var showDate = "",
12       nextShowDate = "";
13 
14   if (nowtoday.getMonth() < 9) showMonth = "0" + showMonth;
15   if (nextday.getMonth() < 9) nextShowMonth = "0" + nextShowMonth;
16 
17   nowtoday.getDate() < 10 ? showDate = "0" + nowtoday.getDate() : showDate = nowtoday.getDate();
18   nextday.getDate() < 10 ? nextShowDate = "0" + nextday.getDate() : nextShowDate = nextday.getDate();
19 
20   return{"nowtoday" : nowtoday.getFullYear() + "-" + showMonth + "-" + showDate , "nextday" : nextday.getFullYear() + "-"+ nextShowMonth + "-" + nextShowDate}
21 }
22 
23 function goHotelInformation(hotelId){
24     var path = location.pathname,
25         idx = path.indexOf("/",1),
26         ctx = path.substring(0,idx);
27     window.open(ctx+"/htl/order/HotelInfomation.do?hotelId="+ hotelId +"&checkInDate="+ getDay().nowtoday +"&checkOutDate="+ getDay().nextday +"&c=1&m=1&L=0");
28 }
29 </script>
原文地址:https://www.cnblogs.com/dtdxrk/p/2749371.html