views:

42

answers:

3

Hi

I have this code in javascript to show time in div and update it:

  if (!document.all)
    return

  var Digital=new Date()
  var hours=Digital.getHours()
  var minutes=Digital.getMinutes()
  var seconds=Digital.getSeconds()
  var dn="AM" 

  if (hours>12){
   dn="PM"
   hours=hours-12
  }

  if (hours==0)
   hours=12

  if (minutes<=9)
   minutes="0"+minutes

  if (seconds<=9)
   seconds="0"+seconds
   var ctime=hours+":"+minutes+":"+seconds
   $("#c").text(ctime);

   setTimeout("show2()",1000)
}

window.onload=show2

and I have this div :

<div id=""c></div>

in IE every thing is ok but when I try it in Firefox, it's not working.

I also tried to use

c.innerHTML=ctime

and it's not working.

What is the right way to do it on both IE and Firefox?

Thanks

+1  A: 

Try <div id="c"></div> and document.getElementById('c').innerHTML = ctime;

and get rid of if (!document.all) return

Fosco
If that $("#c") is JQuery, I think. And it resolves to document.getElementById('c').
RichN
It's tagged jQuery so that should be jQuery :)
Onkelborg
Btw, innerHTML is more dangerous than setting the text since innerHTML doesn't escape anything
Onkelborg
+1  A: 

Remove

  if (!document.all)
    return

since document.all is an IE only extension, and FireFox doesn't support it

Onkelborg
+2  A: 

it's the first line

if (!document.all)
    return

since .all property exists on Explorer and Opera only.

You are preventing other browser to continue code execution

Fabrizio Calderan