![]() |
||||||||
|
||||||||
Week Two JavaScripting NotesTonight we are going to deconstruct some simple JavaScript examples, diagram them, and then look into our first JavaScript library (JQuery). For diagraming purposes, let's use the following color scheme (we can agree to change it in class if you don't like it): Comments grey (606060) Let's start with some examples from a previous group of students' suggestions:
<style> <!-- Paste this code into an external JavaScript file named: timeFormat.js --> function formatTime() { now = new Date(); hour = now.getHours(); min = now.getMinutes(); sec = now.getSeconds(); if (document.clock.sivamtime[0].checked) { if (min <= 9) { min = "0" + min; } if (sec <= 9) { sec = "0" + sec; } if (hour > 12) { hour = hour - 12; add = " p.m."; } else { hour = hour; add = " a.m."; } if (hour == 12) { add = " p.m."; } if (hour == 00) { hour = "12"; } document.clock.sivam.value = ((hour<=9) ? "0" + hour : hour) + ":" + min + ":" + sec + add; } if (document.clock.sivamtime[1].checked) { if (min <= 9) { min = "0" + min; } if (sec <= 9) { sec = "0" + sec; } if (hour < 10) { hour = "0" + hour; } document.clock.sivam.value = hour + ':' + min + ':' + sec; } setTimeout("formatTime()", 1000); } window.onload = formatTime; <!-- Paste this code into the HEAD section of your HTML document. You may need to change the path of the file. --> <script type="text/javascript" src="timeFormat.js"></script> <!-- Paste this code into the BODY section of your HTML document --> <form name="clock"> <table class="clock" width="135"> <tr> <td class="clock2"> Local Time </td> </tr> <tr> <td> <input class="clock2" type="text" name="sivam" size="12"><br> </td> </tr> <tr> <td> <label class="clock3" for="1"><input type="radio" id="1" name="sivamtime" checked>12 Hour Format</label><br> <label class="clock3" for="2"><input type="radio" id="2" name="sivamtime">24 Hour Format</label> </td> </tr> </table> </form> <p><div align="center"> <font face="arial, helvetica" size"-2">Free JavaScripts provided<br> by <a href="http://javascriptsource.com">The JavaScript Source</a></font> </div><p> Here we look at an example suggested by a student named Sarun: Paste the following code before the closing </BODY> tag of the example you've been building above. <!-- JavaScript examples by Quackit.com --> <script type="text/JavaScript"> <!-- function timedRedirect(redirectTo, timeoutPeriod) { setTimeout("location.href = redirectTo;",timeoutPeriod); } // --> </script> <a href="JavaScript:void(0);" onclick="JavaScript:timedRedirect(redirectTo='http://www.quackit.com/html/examples/', timeoutPeriod='2000')">Redirect in 2 seconds...</a> Now, you get to do some diagraming after we look at these examples in class: <script language="javascript" type="text/javascript" > <!-- hide function jumpto(x) { if (document.form1.jumpmenu.value != "null") { document.location.href = x; } } // end hide --> </script> SECOND EXAMPLE FOR YOU TO DIAGRAM: <!-- STEP ONE: Place the following script into a separate JavaScript file called: calendar.js --> var year = Calendar.getYear(); var DAYS_OF_WEEK = 7; Calendar.setDate(1); var TR_start = '<TR>'; cal = '<table border=1 cellspacing=0 cellpadding=0 bordercolor=#BBBBBB><tr><td>'; cal += TD_end + TR_end; for(index=0; index < DAYS_OF_MONTH; index++) { //START FOR LOOP BLOCK if(week_day == 0) { if(week_day != DAYS_OF_WEEK) { var day = Calendar.getDate(); if( today==Calendar.getDate() ) { if(week_day == DAYS_OF_WEEK) { Calendar.setDate(Calendar.getDate()+1); cal += '</td></tr></table></table>'; document.write(cal); <!-- STEP TWO: Place this into the BODY of the HTML document where you want the calendar --> <script src="calendar.js"></script> |
||||||||