Introduction
Need a quick, easy to use, self setting date selector for use on Web pages? Here is a drop down calendar that fits the bill! You can see my Calender demo page here.
Background
I have looked around for a date selector to use on Web pages for a long time. A couple of years ago, I found datetimepicker.js. A popup window date time selector written by TengYong Ng in 2003 (See Web site). I used it in several Web applications, however, the problem with popup Windows is that they can get lost. If the user clicks somewhere else, it can end up behind other Windows and will not re-appear when the link is clicked again. While making my way through some Ajax tutorials, I wondered if I could take the datetimepickers JavaScript built HTML code and stuff it into the innerHTML of a page element. So I stripped out all the window stuff and removed the time entry pieces. I added code for auto creation and deletions of a div element and modified how months and years are navigated. This drop down calendar is the result.
After getting a message that FireFox was not a happy browser with this script, I began my search for what will make it happy. Well, after 12 hours of Googling, testing, reading, retesting and finally some good old trial and error, it seems to do everything it is supposed to for both FireFox and Internet Explorer 7. It is amazing how messy things get when you are trying to make something work across browsers. I was having flashbacks of trying to make DOS programs that also ran on UNIX. For example, in Internet Explorer, the div element is created and appended to the body and you can get a height value from Cal.elem.offsetHeight right away, but in FireFox, this value remained zero forever. Not 'undefined', but zero (0). So I wrapped the main table in the div in another table with an id='calT1'. When the div is appended to the body, FireFox does report document.getElementById('calT1').offsetHeight with the correct height. 97% of my day is writing VC++ code for data recording and reporting in Microsoft OS environments. One day I said to a customer 'Oh, I can put all these reports out on the Web for you. Then you can access them from anywhere in the world!'. Now I know why most Web apps say Internet Explorer Vx or Netscape Vxx required. You could spend your career keeping up with browsers and scripts.
8/3/07 - After hearing a lot about 'Select Control Bleed Through' and getting some input from Thorium, I added code to hide the select controls that would be partially or completely covered by the calendar. Those that are completely covered work just fine, but if you have a select control that would be partially covered, then expect to see it disappear while the calendar is open and then reappear when the calendar closes. Since this problem only occurs in Microsoft Internet Explorer 6 or below, the code will ignore the select controls if the browser is not Internet Explorer or version 7 and above.
3/5/08 - I got many requests from our non US readers for a method to change the starting day of the week. I finally found some time to revisit this and it really didn't take much code to make it happen. In the variable declarations, there is now a var named WeekStartsOnDay. If you set it to zero (0) then the first day is Sunday, set it to one (1) and the first day is Monday. I haven't tested it much past that, but then is there really a need?
Using the Code
As with any external script file, we have to get it included in our page. This goes in the head section like this:
<head>
// .... other head stuff ...
<script type="text/javascript" src="DropCalendar.js"></script>
</head>
To make it work, all you need is a text box with a name* or id assigned and a hyperlink to call the JavaScript. Here I'm using an image as the hyperlink to open the calendar.
(*FireFox and the like really want an id, not a name.)
<body>
<input type="text" id="T2" size="14"><a href="javascript:OpenCal('T2');">
<img border="0" src="cal.gif" width="16" height="16"></a>
</body>
That's all there is to it. You can change some of the characteristics of the calendar by modifying the global variables found in the script file. Things that can be changed are colors, size of the drop down, font size and default format. You can also change the format of the date display by sending it as a second argument. The default is mmddyyyy. Don't include any separators like slashes '/' in the format.
Points of Interest
The calendar will always show up attached to the bottom left corner of the text box, unless it would be off the page. It will slide to the left if it would have gone off the right side and it will be attached to the top of the text box if it would have gone off the bottom of the browser.
Navigation of months and years is via the << < and > >> hyperlinks on the calendar.
The user can close the calendar by clicking the X in the top right corner or by clicking the hyperlink image that originally opened it.
The calendar drop down box is like Highlander, there can be only one. If you click a second link on a page that has multiple calendar links on it, the calendar closes its original position and moves to the new one.
The script basically creates the body of an HTML page made up of tables of hyperlinks that calls functions to navigate dates or select them and puts the selection in the text box via its value member. When you call OpenCal, it creates a div element on your page and stuffs the HTML calendar into its innerHTML member and appends it to the document.body collection.
When the calendar is closed, the div element is removed from the document.body via the removeElement method. The close function then deletes the calendar which destroys the div element along with it.
| You must Sign In to use this message board. |
|
|
 |
|
 |
Excellent, thank you! However, how would you suggest use it in Gridview (or ListView) on row updating?
vad
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Something like this doesn't work.
<a href="javascript:OpenCal('<%=txtEndDate.ClientID%>');"><asp:Image ID="imgCal" runat="server" ImageUrl="~/images/cal.gif" /></a> <asp:TextBox runat="server" id="txtEndDate" Width="80px" />
vad
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi! Great script...but I have an issue.
When I create a standalone php-page containing a form and this calendar script, everything works just fine. The calendar opens right below the text box and populates it like it should.
However, when I try to load this php-page into a div dynamically using ajaxpage, things go wrong. Then script creates the calendar, but in the upper left corner of the browser window, way outside the div the php-page is dynamically loaded into.
I can create a div around the calendar (within the DropCalender.js code) and force it to another location, but then it gets behind other objects and becomes unclickable - and I cannot seem to fix this with Z-index (which is a known issue when loading pages dynamically). It also does not adjust properly when I resize the browser window.
Why do you think the js-code (which is properly appended to the php-page using loadobjs) does not generate the calender in the correct position, and on top of every other object, when used together with ajax-page?
Regards Staland
Staland Hoerveer
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
hi, i fix this problem for u (i was need this to ), u just replace the last function to this one and all will work like u need:
function FormatDate(pDate) { var tMonth = (this.Month + 1 < 10) ? '0' + (this.Month +1) : tMonth = this.Month +1; var tDate = (pDate < 10) ? '0' + pDate : pDate;
if (this.Format.toUpperCase()=="DDMMYYYY") return (tDate+DateSeparator+(tMonth)+DateSeparator+this.Year); else if (this.Format.toUpperCase()=="DDMMMYYYY") return (tDate+DateSeparator+this.GetMonthName(false)+DateSeparator+this.Year); else if (this.Format.toUpperCase()=="MMDDYYYY") return ((tMonth)+DateSeparator+tDate+DateSeparator+this.Year); else if (this.Format.toUpperCase()=="MMMDDYYYY") return (this.GetMonthName(false)+DateSeparator+tDate+DateSeparator+this.Year); }
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
If you set WeekStartsOnDay to 1 (from 0), and you specify different colours for SundayColor, SaturdayColor and WeekDayColor, the wrong columns are shaded
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
If I give the date format as like (DDMMMYY)this means error is showing. how to achive this ple explain.eg(02-MAR-08)
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
|
 |
|
 |
I had an issue with placement of the the calendar in Firefox. I found that line 258 was missing a couple of "px" elements. The corrected line is shown below.
Cal.elem.setAttribute("style","position:absolute; top:"+bottom+"px; left:"+leftpos+"px; z-index:1; width:" + cnWidth + "px; height:0px;");
This is a great widget!!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
My web using ASP.net. It's work well in IE7 but in firefox the calendar appear at the bottom of the page. Please help!
Something should remember but some thing should forget. Your life will brighter
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
|
 |
Also have the same problem..
from running through the example index.html included with the zip, and through the aspx page that I have in the Venkmann debugger I'm struggling to see what's different.
Both are calculating roughly the same screen position, so it's not that. I think it has something to do with the document.body.appendChild, does Firefox treat the position relative to the bottom of the document body - even though it is declared absolute?
I'm a little stuck
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Here's the solution .. -----------------------------------
add the following lines:
Cal.elem.style.top = bottom + "px"; Cal.elem.style.left = leftpos + "px";
BEFORE the line that reads:
var oSelects=document.getElementsByTagName("select");
also change the z-index to 200, from 1
- what is the block that's above this meant to do? The above seems to work fine in both IE and firefox.
|
| Sign In·View Thread·PermaLink | 4.00/5 |
|
|
|
 |
|
 |
I found your control and it's exaclty what I was looking for but I need to implement it in an ASP.NET VB application. I'm having trouble calling it from the details view that I set up. Here is the code I'm trying to use: <asp:TemplateField HeaderText="Date of Service" SortExpression="DATE_OF_SERVICE"> <EditItemTemplate> <asp:Label ID="Label3" runat="server" Text='<%# Eval("DATE_OF_SERVICE") %>'></asp:Label> </EditItemTemplate> <InsertItemTemplate> <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("DATE_OF_SERVICE") %>'></asp:TextBox> <asp:HyperLink runat="server" NavigateUrl="javascript:OpenCal('<%= TextBox5.ClientID) %>');" ID="DATE_OF_SERVICE"><asp:Image ID="Image1" runat="server" ImageUrl="Images/cal.gif" /></asp:HyperLink> </InsertItemTemplate> <HeaderStyle HorizontalAlign="Left" /> <ItemTemplate> <asp:Label ID="Label5" runat="server" Text='<%# Bind("DATE_OF_SERVICE") %>'></asp:Label> </ItemTemplate> </asp:TemplateField>
Any suggestion on how to make this work?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
try including the full path to the .js file in the <script> tag. SEE "Just Excellent" by forozco in comments on this page
The resolution is the responsibility of all with the ability.
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
|
 |
Is there a way to disable certain days of the week/year? I'd like to only allow people to choose weekdays, and not choose holidays (eg Christmas). Is there a way to configure this on the fly?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Sorry, Not at this time. You might do some validation on your page and alert the user they have selected an invalid date. Google 'ASP Form Validation' for examples.
The resolution is the responsibility of all with the ability.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
That's one way of handling it, but is not ideal. I'd rather prevent someone from doing something wrong rather than telling them after the fact. Way more user friendly.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Just found small error - when you choose date "29.2.2008" (or other leap year), the date is correctly saved to the text field, but when you open calendar again, selected date is "9.2.2008". It is not so important, but if you will have some spare time...
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
This error occoured because the script was checking for leap year before it parsed out the year. This has been corrected and can can be downloaded here and from the demo page
The resolution is the responsibility of all with the ability.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Thanks a lot for this great job. I have only one problem - how can I set first day of week other then Sunday? In my country is default Monday.
|
| Sign In·View Thread·PermaLink | 2.50/5 |
|
|
|
 |
|
 |
Excellent control!!
I added a couple of things to it, to allow for the calendar to close when the mouse moves out of the calendar.
var blnCalendarVisible = false; function bodyMouseOver() { if (!blnCalendarVisible) return; var target = this.event.srcElement; while (target.id != 'BODY') { target = target.parentNode; if (target == null) return; if (target.tagName == 'DIV' && target.id == 'cal') return; if (target.tagName == 'DIV') { blnCalendarVisible = false; CloseCal(); return; } } }
function calMouseOver() { if (blnCalendarVisible) return; blnCalendarVisible = true; }
First you declare another global variable "blnCalendarVisible" that gets set when the user first moves the mouse over the calendar. Then when the user moves out of the calendar the "bodyMouseOver" function closes the calendar.
In the calendar control the following changes need to be made: Cal.elem = document.createElement('<div id="cal" onmouseover="calMouseOver()" style="position:absolute; top:'+ bottom + 'px; left:' + leftpos + 'px; z-index:1; width:' + cnWidth + 'px; height:0px;"></div>'); //added mouseover event
wrap the "CloseCal()" function in a try/catch routine: function CloseCal() { try { document.body.removeChild(Cal.elem); document.body.style.overflow = BodyOverflow; delete Cal; Cal = null; delete selectControls; showSelects(); } catch (e){} }
Finally in the "Body" tag add this entry. <BODY onmouseover="bodyMouseOver()">
|
| Sign In·View Thread·PermaLink | 3.00/5 |
|
|
|
 |
|
 |
Hi dear friend, it is excellent , but I have a question: Is it possible to change calendar format? for example I want to show Shamsi(Jalali) Calendar instead of this!
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
 |
I did some looking around for what would be involved to fullfil your request and decided that it would take much more time then I have to give to this little control at the moment. Perhaps in the future.
The resolution is the responsibility of all with the ability.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
 it's workn fine n a great job done by you.
but i have a little problem that when the calendar is popup then other controls like dropdownlist control overlap on it and it popuped sometime abov and sometime below the control automatically. can we control these issues?????
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|