2013年4月23日 星期二

自製javascript 檢查時間功能

From Evernote:

自製javascript 檢查時間功能

與檢查日期功能相同原理。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=big5">
  <title></title>
  </head>
  <body>
<script>

function chk_time()
{
    //結果顯示物件初始
    result_obj = document.getElementById("result");
    result_obj.innerHTML="";
    //取得輸入時間字串
    d=document.getElementsByName('enter_time')[0].value;
    //若時間字串非數值或不等於HHMMSS就結束
    if(isNaN(d) || d.length != 6)
    {
        result_obj.innerHTML="時間格式有誤(HHMMSS)";
        return false;
    }
    //用時間字串產生時間物件,兩者再分別比對時分秒是否一致
    a = new Date();
    a.setHours(parseInt(d.substr(0,2)));
    a.setMinutes(parseInt(d.substr(2,2)));
    a.setSeconds(parseInt(d.substr(4,2)));

    result_obj.innerHTML="時間物件:"+a+"<br>輸入時間:"+d;
    result_obj.innerHTML+="<br>物件的時:"+a.getHours()+"&nbsp;&nbsp;輸入的時:"+ parseInt(d.substr(0,2));
    result_obj.innerHTML+="<br>物件的分:"+(a.getMinutes())+"&nbsp;&nbsp;輸入的分:"+ parseInt(d.substr(2,2));
    result_obj.innerHTML+="<br>物件的秒:"+a.getSeconds()+"&nbsp;&nbsp;輸入的秒:"+ parseInt(d.substr(4,2));

    //比對時
    if(a.getHours() != parseInt(d.substr(0,2)))
    {
        result_obj.innerHTML+="<BR>輸入時間有誤";
    }
    //比對分
    else if(a.getMinutes() != parseInt(d.substr(2,2)))
    {
        result_obj.innerHTML+="<BR>輸入時間有誤";
    }
    //比對秒
    else if(a.getSeconds() != parseInt(d.substr(4,2)))
    {
        result_obj.innerHTML+="<BR>輸入時間有誤";
    }
    else
    {
        result_obj.innerHTML+="<BR>輸入時間正確";
    }


}
</script>
<input type="text" size="20" name="enter_time">
<input type="button" value="submit" onClick="chk_time()">
<br>
<div id="result">
</div>
  </body>
</html>

2013年4月22日 星期一

自製javascript 檢查日期功能

From Evernote:

自製javascript 檢查日期功能

原理:將日期丟到Date()產生日期物件,若是有問題的日期,從物件取出的年月日或與原來輸入的不一樣,同樣方法可以套用在時間檢查上。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=big5">
  <title></title>
  </head>
  <body>
<script>

function chk_date()
{
    //結果顯示物件初始
    result_obj = document.getElementById("result");
    result_obj.innerHTML="";
    //取得輸入日期字串
    d=document.getElementsByName('enter_date')[0].value;
    //若日期字串非數值或不等於YYYYMMDD就結束
    if(isNaN(d) || d.length != 8)
    {
        result_obj.innerHTML="日期格式有誤(YYYYMMDD)";
        return false;
    }
    //用日期字串產生日期物件,兩者再分別比對年月日是否一致
    a = new Date(parseInt(d.substr(0,4)),parseInt(d.substr(4,2))-1,parseInt(d.substr(6,2)));

    result_obj.innerHTML="日期物件:"+a+"<br>輸入日期:"+d;
    result_obj.innerHTML+="<br>物件的年:"+a.getFullYear()+"&nbsp;&nbsp;輸入的年:"+ parseInt(d.substr(0,4));
    result_obj.innerHTML+="<br>物件的月:"+(a.getMonth()+1)+"&nbsp;&nbsp;輸入的月:"+ parseInt(d.substr(4,2));
    result_obj.innerHTML+="<br>物件的日:"+a.getDate()+"&nbsp;&nbsp;輸入的日:"+ parseInt(d.substr(6,2));

    //比對年
    if(a.getFullYear() != parseInt(d.substr(0,4)))
    {
        result_obj.innerHTML+="<BR>輸入日期有誤";
    }
    //比對月
    else if(a.getMonth() != (parseInt(d.substr(4,2))-1))
    {
        result_obj.innerHTML+="<BR>輸入日期有誤";
    }
    //比對日
    else if(a.getDate() != parseInt(d.substr(6,2)))
    {
        result_obj.innerHTML+="<BR>輸入日期有誤";
    }
    else
    {
        result_obj.innerHTML+="<BR>輸入日期正確";
    }


}
</script>
<input type="text" size="20" name="enter_date">
<input type="button" value="submit" onClick="chk_date()">
<br>
<div id="result">
</div>
  </body>
</html>

2013年4月10日 星期三

用javascript偵測彈出式視窗是否被封鎖

From Evernote:

用javascript偵測彈出式視窗是否被封鎖

new_window_obj = window.open([url],[window name])
if(null == new_window_obj)
{
     //如果沒開啟新視窗就進入if
}

在封鎖彈出式視窗的情況下,IE 10與firefox 21適用上面方法偵測;
chrome會開啟彈出式視窗但不顯示,也就是window物件與網頁內容都建立了,我還找不出方法來偵測是否封鎖。

2013年4月2日 星期二

取得html標籤中所有內容

From Evernote:

取得html標籤中所有內容

Clipped from: http://www.lslnet.com/linux/f/docs1/i04/big5125741.htm
為了做出另開新視窗,再將父視窗內容複製到子視窗的功能,
之前只會:子視窗名稱.document.body.innerHTML=document.body.innerHTML; 複製body裡面的東西。
在head標籤中的css都沒複製到,後來google到下面這個網址:
http://www.lslnet.com/linux/f/docs1/i04/big5125741.htm
網頁中有以下幾個方法:
d ocument.body.parentNode.innerHTML
d ocument.body.parentElement.innerHTML
d ocument.getElementsByTagName("html")[0].innerHTML
d ocument.all.tags("html")[0].innerHTML
d ocument.documentElement.innerHTML