javascript: making a calendar navigation bar -
when click on function goes wrong. problem. struggle few month on this. click on button , exepect month number increase or decrease. , show me right result.
<!doctype html> <html> <style> table { border-collapse:collapse; } table img { width:50px; height:50px; } td,table { border-color:blue; text-align:center; } td:hover { cursor:pointer; background-color:yellow; } </style> <head> </head> <body> <script> var date=new date(); var mn=['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec']; y=date.getfullyear(); m=date.getmonth(); function preyear() { y -= 1; calendar(); } function premonth(){ m -= 1; calendar(); } function nextmonth(){ m += 1; calendar(); } function nextyear(){ y += 1; calendar(); } function today(){ y=date.getfullyear(); m=date.getmonth(); calendar(); } function calendar(){ document.write("<table border=1><tr><td colspan=8>"+mn[m]+' '+y+"</td></tr>"); document.write("<tr><td colspan='8'><button value='preyear' onclick='preyear()'><button value='premonth' onclick='premonth()'><button value = 'today' onclick = 'today()'><button value = 'nextmonth' onclick='nextmonth()'><button value = 'nextyear' onclick = 'nextyear()'></td></tr>"); document.write ("<tr><td>s</td><td>m</td><td>t</td><td>w</td><td>t</td><td>f</td><td>s</td><td>w</td></tr>"); document.write("<tr>"); (var i=1; i<43; i++){ var fday=new date(y,m,1); days=new date(y,m,i-fday.getday()); if (days.todatestring()== date.todatestring()){ document.write("<td style=color:red>"+days.getdate()+"</td>"); } else{ document.write("<td>"+days.getdate()+"</td>"); } if (i%7 == 0){ var onejan=new date(y,0,1); document.write("<td>"+math.ceil(((days-onejan)/(60*60*24*1000)+1)/7) +"</td></tr><tr>"); } } document.write("</tr></table>"); } calendar(); </script> </body> </html>
when using document.write second time calender function called, rewriting whole html , <head>
tags gets lost.
that happens when use document.write after page finished loading, should use instead:
<!doctype html> <html> <style> table { border-collapse:collapse; } table img { width:50px; height:50px; } td,table { border-color:blue; text-align:center; } td:hover { cursor:pointer; background-color:yellow; } </style> <head> </head> <body> <script> var date=new date(); var mn=['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec']; y=date.getfullyear(); m=date.getmonth(); function preyear() { y -= 1; calendar(); } function premonth(){ m -= 1; calendar(); } function nextmonth(){ m += 1; calendar(); } function nextyear(){ y += 1; calendar(); } function today(){ y=date.getfullyear(); m=date.getmonth(); calendar(); } function calendar(){ var html = ""; html += "<table border=1><tr><td colspan=8>"+mn[m]+' '+y+"</td></tr>"; html += "<tr><td colspan='8'><button value='preyear' onclick='preyear()'><button value='premonth' onclick='premonth()'><button value = 'today' onclick = 'today()'><button value = 'nextmonth' onclick='nextmonth()'><button value = 'nextyear' onclick = 'nextyear()'></td></tr>"; html += "<tr><td>s</td><td>m</td><td>t</td><td>w</td><td>t</td><td>f</td><td>s</td><td>w</td></tr>"; html += "<tr>"; (var i=1; i<43; i++){ var fday=new date(y,m,1); days=new date(y,m,i-fday.getday()); if (days.todatestring()== date.todatestring()){ html += "<td style=color:red>"+days.getdate()+"</td>"; } else{ html += "<td>"+days.getdate()+"</td>"; } if (i%7 == 0){ var onejan=new date(y,0,1); html += "<td>"+math.ceil(((days-onejan)/(60*60*24*1000)+1)/7) +"</td></tr><tr>"; } } html += "</tr></table>"; document.body.innerhtml = html; } calendar(); </script> </body> </html>
Comments
Post a Comment