JavaScript [jQuery] 자바스크립트와 제이쿼리를 이용하여 나이를 구하는 방법
페이지 정보
본문
<script src="//code.jquery.com/jquery-1.8.3.min.js" type="text/javascript"></script>
<script>
$(window).load(function(){
var dob = $('.age').data('age');
var xmlHttp;
function srvTime(){
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
xmlHttp.open('HEAD',window.location.href.toString(),false);
xmlHttp.setRequestHeader("Content-Type", "text/html");
xmlHttp.send('');
return xmlHttp.getResponseHeader("Date");
}else if (window.ActiveXObject) {
xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
xmlHttp.open('HEAD',window.location.href.toString(),false);
xmlHttp.setRequestHeader("Content-Type", "text/html");
xmlHttp.send('');
return xmlHttp.getResponseHeader("Date");
}
}
var st = srvTime();
if(dob != ''){
var str=dob.split('-');
var today = new Date(st);
var toMonth = today.getMonth()+1;
var toDate = today.getDate();
var toYear = today.getFullYear();
var toAge = toYear - str[0];
if (toMonth > str[1]){
dayDiff = toAge;
} else if (str[1] > toMonth) {
dayDiff = toAge -1;
} else {
if (str[2] > toDate) {
dayDiff =toAge -1;
} else {
var dayDiff = toAge;
}
}
var age = parseInt(dayDiff);
$('.age').html('( ' + toYear + '년 현재 : ' + age + '세 )');
}
});
</script>
<input type="text" name="mb_birth" value="<?php echo $mb['mb_birth']; ?>" class="frm_input" size="15">
<span data-age="<?php echo $mb['mb_birth']; ?>-00-00" class="age"></span>
자료출처
http://oa-tools.blogspot.com/2015/09/jquery-javascript.html
<script>
$(window).load(function(){
var dob = $('.age').data('age');
var xmlHttp;
function srvTime(){
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
xmlHttp.open('HEAD',window.location.href.toString(),false);
xmlHttp.setRequestHeader("Content-Type", "text/html");
xmlHttp.send('');
return xmlHttp.getResponseHeader("Date");
}else if (window.ActiveXObject) {
xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
xmlHttp.open('HEAD',window.location.href.toString(),false);
xmlHttp.setRequestHeader("Content-Type", "text/html");
xmlHttp.send('');
return xmlHttp.getResponseHeader("Date");
}
}
var st = srvTime();
if(dob != ''){
var str=dob.split('-');
var today = new Date(st);
var toMonth = today.getMonth()+1;
var toDate = today.getDate();
var toYear = today.getFullYear();
var toAge = toYear - str[0];
if (toMonth > str[1]){
dayDiff = toAge;
} else if (str[1] > toMonth) {
dayDiff = toAge -1;
} else {
if (str[2] > toDate) {
dayDiff =toAge -1;
} else {
var dayDiff = toAge;
}
}
var age = parseInt(dayDiff);
$('.age').html('( ' + toYear + '년 현재 : ' + age + '세 )');
}
});
</script>
<input type="text" name="mb_birth" value="<?php echo $mb['mb_birth']; ?>" class="frm_input" size="15">
<span data-age="<?php echo $mb['mb_birth']; ?>-00-00" class="age"></span>
자료출처
http://oa-tools.blogspot.com/2015/09/jquery-javascript.html
댓글목록
등록된 댓글이 없습니다.