Javascript – Age Caluculator, Caluculating Age, How to calculate age in javascript.

Below codes divided into two parts, one part in head tag and another tag in body. 

<html>
<head>
<!– AGE CALCULATOR –>

<script language=”JavaScript” type=”text/javascript”>

<!–
function getAge() {
 month = (document.form1.month.value – 1);
 date = document.form1.date.value;
 year = document.form1.year.value;

 if (month != parseInt(month)) { alert(‘Type Month of birth in digits only!’); return false; }
 if (date != parseInt(date)) { alert(‘Type Date of birth in digits only!’); return false; }
 if (year != parseInt(year)) { alert(‘Type Year of birth in digits only!’); return false; }
 if (year.length < 4) { alert(‘Type Year of birth in full!’); return false; }

 today = new Date();
 dateStr = today.getDate();
 monthStr = today.getMonth();
 yearStr = today.getFullYear();

 theYear = yearStr – year;
 theMonth = monthStr – month;
 theDate = dateStr – date;

 var days = “”;
 if (monthStr == 0 || monthStr == 2 || monthStr == 4 || monthStr == 6 || monthStr == 7 || monthStr == 9 || monthStr == 11) days = 31;
 if (monthStr == 3 || monthStr == 5 || monthStr == 8 || monthStr == 10) days = 30;
 if (monthStr == 1) days = 28;

 document.form1.inYears.value = theYear;

 if (month < monthStr && date > dateStr) { document.form1.inYears.value = parseInt(document.form1.inYears.value) + 1;
                                           document.form1.inMonths.value = theMonth – 1; }
 if (month < monthStr && date <= dateStr) { document.form1.inMonths.value = theMonth; }
 else if (month == monthStr && (date < dateStr || date == dateStr)) { document.form1.inMonths.value = 0; }
 else if (month == monthStr && date > dateStr) { document.form1.inMonths.value = 11; }
 else if (month > monthStr && date <= dateStr) { document.form1.inYears.value = document.form1.inYears.value – 1;
                                                 document.form1.inMonths.value = ((12 – -(theMonth)) + 1); }
 else if (month > monthStr && date > dateStr) { document.form1.inMonths.value = ((12 – -(theMonth))); }

 if (date < dateStr) { document.form1.inDays.value = theDate; }
 else if (date == dateStr) { document.form1.inDays.value = 0; }
 else { document.form1.inYears.value = document.form1.inYears.value – 1; document.form1.inDays.value = days – (-(theDate)); }
  }
// –>
</script>

</head>
<body>

<form name=”form1″>
<table align=”center” width=”380″ border=”0″ cellpadding=”0″ cellspacing=”0″>
 <tr>
  <td align=”center”>
   MM: <input type=”text” maxLength=”2″ size=”2″ name=”month” value=”01″ /> 
   DD: <input type=”text” maxLength=”2″ size=”2″ name=”date” value=”30″ /> 
   YYYY: <input type=”text” maxLength=”4″ size=”4″ name=”year” value=”1990″ />
   <input type=”button” value=”Get Age” onClick=”getAge()” /><br /><br />
  </td>
 </tr>
 <tr>
  <td align=”center”>
   <table border=”0″ cellpadding=”0″ cellspacing=”0″ width=”120″>
    <tr><td align=”right”><input type=”text” size=”2″ name=”inYears” value=”” /></td><td align=”left”>Years</td></tr>
    <tr><td align=”right”><input type=”text” size=”2″ name=”inMonths” value=”” /></td><td align=”left”>Months</td></tr>
    <tr><td align=”right”><input type=”text” size=”2″ name=”inDays” value=”” /></td><td align=”left”>Days</td></tr>
   </table>
  </td>
 </tr>
</table>
</form>

</body>
</html>

Click here to download source code Age calculator

Advertisements

Javascript – Display alternate message in textbox

You can set timeout as your wish.

———————————————————————-

<html>

<script language=”JavaScript” type=”text/javascript”>
<!–

var text = 0;

var message=new Array();
message[0] = “Welcome to Rana’s Blog Code Project!”
message[1] = “Get free source code from Rana’s Blog.”
message[2] = “Solution will be provided on request.”
message[3] = “Good bye from Rana’s blog code project.”

function changeText() {
  if (message.length > 0) {
   document.change.descript.value=message[text];
   text++; }
  if (text == 4) {text = 0; }  // change the # 4 at the left to the maximum # of message lines you want included
   window.setTimeout(“changeText()”, 3500); }  // change the # on the left to adjust the speed of the
                                               // scroll. The smaller the # the faster the speed
//–>
</script>
<body onLoad=”changeText();”>

<!– This goes into the BODY of the html file –>

<form name=”change”>
  <textarea name=”descript” rows=”2″ cols=”33″ wrap=”virtual”></textarea>
</form>

</body>
</html>

Click here to download the source code : Displaying alternate messages

Javascript – Click here to make default homepage.

Here is the code to make your site as default hompage. 

<html>
<body>
<a href=”#” mce_href=”#” onClick=”this.style.behavior=’url(#default#homepage)’;this.setHomePage(‘https://codeproject.wordpress.com&#8217;);”>Click here to make Rana’s blog  your default homepage</a>

</body>
</html>

Note : Make sure when you copy the text and pasted on your source code window, replace ”  with ” and single comma with ‘

Should look like below image.
Make a rana blog as default home page