Home arrow JavaScript arrow Page 5 - Useful Web Widgets
JAVASCRIPT

Useful Web Widgets


If you do a lot of web programming, it can save you a lot of time and effort to have a library of JavaScript functions. This article presents some useful, reusable client side code, for calendars and more.

Author Info:
By: Chris Root
Rating: 4 stars4 stars4 stars4 stars4 stars / 6
January 18, 2006
TABLE OF CONTENTS:
  1. · Useful Web Widgets
  2. · Getting Down With Father Time
  3. · The Function in Depth
  4. · A Calendar Engine
  5. · The Interface Code

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Useful Web Widgets - The Interface Code
(Page 5 of 5 )

As I mentioned before the calendar object can be used in a lot of environments. The following function is browser DOM specific and shown here as just an example of how to use the data generated. It's been tested in Firefox and Safari, but may work fine in most if not all browsers. This script does not provide a table above for display of the name of the days or a display of the month. All that is up to you. I have the calendar engine code in a separate document and embed it using a "<script>" tag. If you are trying to put together a complete solution you may want to put all the code in a separate document.

We first get a reference to the table which will house the calendar, and then get a calendar object. We also need to get together a few DOM objects to clone while constructing the rows of days. Make sure to set class, align and other attributes you may need to massage the appearance of the table at this point.

function getCalendarMonth(data,selectedYear,selectedMonth)
{
            var table = document.getElementById("calendarTable");
            var cal = new calendar(selectedYear,selectedYear);
            var month = cal.years[selectedYear].monthObjects
[selectedMonth];
            var len = month.dayObjects.length;
            var trow = document.createElement("tr");
            trow.setAttribute("class","calRow");
            var tcell = document.createElement("td");
            tcell.setAttribute("valign","top");
            var temprow = trow.cloneNode(true);
            var eventContainer = document.createElement("div");

Now it's time to loop through the days of the month, adding the event information and constructing the table markup. Depending on how big you want to make the calendar you may want to simply put links in each day pane that produce a popup containing the day's events.

var tempCell = null;
var counter = 0;
var dnum;
for(var c = 0;c < len;c++)
{
    if(counter == 7)
    {
        //we've put seven cells in the current row lets start again.
        counter = 0;
    }
    if(counter == 0)
    {
       
//new row
        temprow = trow.cloneNode(true);
    }
    //new cell
    tempCell = tcell.cloneNode(true);
    dobj = month.dayObjects[c];
    //event information
    if(typeof data[dobj.displayNumber] != "undefined")
    {
        var devent = data[dobj.displayNumber];
    }
    else
    {
        var devent = "";
    }
    temptxt = document.createTextNode(dobj.displayNumber);
    eventText = document.createTextNode(devent);
    container = eventContainer.cloneNode(false);
    container.appendChild(eventText);
    tempCell.appendChild(temptxt);
    tempCell.appendChild(container);
    if(dobj.displayNumber == "")
    {
        //make a blank cell and set a CSS selector

    tempCell.setAttribute("class","blankcell");
        }
        else
        {
            //put the day number in and set a CSS selector
            tempCell.setAttribute("class","daycell");
        }
        //add the new cell to the current row
        temprow.appendChild(tempCell);
        // if we're done inserting all the cells in the row, add it to the table.
        if(counter == 6)
        {
            table.appendChild(temprow);
        }
        counter++;
    }
}

Your Time is up

These three scripts should give you a good start or be a useful addition to an already established library of JavaScript functions. Each can be modified and expanded to fit more situations. If you have any ideas on mods please share them.


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks
- Dynamic jQuery Styling

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials