Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 17th, 2006, 9:53 AM   #1
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,086
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Compatibility of JS Clock

I have a clock working in IE. I am using a generalised "get_object" function which should be compatible with all standard browsers. However, on FireFox it says that the object doesn't have any properties.

head.js
/* GET AN OBJECT */

var objects = new Array();
var type = 0;
if (document.getElementById)
{
    type = 1;
}
else if (document.all)
{
    type = 2;
}
else if (document.layers)
{
    type = 3;
}
function get_object(idname)
{
    if (typeof(objects[idname]) == "undefined")
    {
        if (type == 1)
        {
            objects[idname] = document.getElementById(idname);
        }
        else if (type == 2)
        {
            objects[idname] = document.all[idname];
        }
        else if (type == 3)
        {
            objects[idname] = document.layers[idname];
        }
    }
    return objects[idname];
}

/* HIDE/SHOW AN OBJECT */

function hide(contID, imgID, hideurl, showurl)
{
    dynobj = get_object(contID)
    dynimg = get_object(imgID)

    if (dynobj.style.display == "")
    {
        dynobj.style.display = "none";
        dynimg.src = hideurl;
    }
    else
    {
        dynobj.style.display = "";
        dynimg.src = showurl;
    }

    return false;
}

/* CLOCK */

clockobj = new Array();

function getclock()
{
    for (i=0;i<=5;i++)
    {
      clockobj[i] = get_object('t'+i);
    }
    doclock();
}

function doclock()
{
    var thetime = new Date();
    var parts = new Array();

    parts[0] = mk_length(thetime.getHours(), 2);
    parts[1] = mk_length(thetime.getMinutes(), 2);
    parts[2] = mk_length(thetime.getSeconds(), 2);

    for (i=0;i<=2;i++)
    {
      clockobj[i*2].src = "/images/clock_"+parts[i].substring(0,1)+".png";
      clockobj[i*2+1].src = "/images/clock_"+parts[i].substring(1,2)+".png";
    }

    setTimeout('doclock()',1000); 
}

/* GLOBAL */

function mk_length(text, length)
{
    text = text + ''
    var tlength = text.length;
    if (tlength < length)
    {
      text = '0'*(length-tlength) + text;
    }
    return text;
}

html
      <img name="t0" alt="Hours" title="Hours" src="/images/clock_0.png" /><img name="t1" alt="Hours" title="Hours" src="/images/clock_0.png" />
      <img name="t2" alt="Minutes" title="Minutes" src="/images/clock_0.png" /><img name="t3" alt="Minutes" title="Minutes" src="/images/clock_0.png" />
      <img name="t4" alt="Seconds" title="Seconds" src="/images/clock_0.png" /><img name="t5" alt="Seconds" title="Seconds" src="/images/clock_0.png" />
Sane is offline   Reply With Quote
Old Apr 17th, 2006, 7:54 PM   #2
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 893
Rep Power: 4 The Dark is on a distinguished road
If you are calling "getElementById", you will need to give your elements an "id" instead of a "name".
The Dark is offline   Reply With Quote
Old Apr 17th, 2006, 8:06 PM   #3
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,086
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
OH F#@(*&@#. I can't believe I missed that!

Thanks Dark. I was wondering why it was working everywhere except my clock. Just got to switch name to ID.

=}
Sane is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:04 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC