View Full Version : Help with small code plz

01-09-2012, 10:53 PM
Hey all, I have this code on my site that simply says if we are open or not.

<script language="Javascript">
Credit must stay intact
Visit java-scripts.net or
http://wsabstract.com for this script

now = new Date

if (now.getHours() <21) {
document.write("We are Currently : Open")

else if (now.getHours() <9)
{* * document.write("We are currently : Closed")}

{ * *document.write("We are currently : Closed")}

I wish to know how I can change this code so that I can add some colour and change the size of the font.

Any help would be great.

Old Pedant
01-10-2012, 02:03 AM
Ummm...I don't think that code works right.

What are the * * characters doing in there before the 2nd and 3rd document.write?

And ignoring them, the code will say you are OPEN from midnight to 9PM. So you would only show CLOSED from 9PM to midnight.

Is that really right?

Anyway, the right answer to anything like this is to *NOT* use document.write. In general, be suspicious of any code that uses document.write. It *can* be the correct answer, but I would guess that 90% of the time it is not.

How about something like this:

<style type="text/css">
#openSign.OPEN {
color: green;
background-color: yellow;
font-size: x-large;
#openSign.CLOSED {
color : red;
background-color: pink;
font-size: large;
<script type="text/javascript">
var OPENAT = 10; // 10:00 AM ... change as appropriate
var CLOSEAT = 21; // 9:00 PM ... change as appropriate

function areWeOpen( )
var sign = document.getElementById("openSign");
var hour = ( new Date() ).getHours;
if ( hour >= OPENAT && hour < CLOSEAT )
sign.innerHTML = "We are now OPEN";
sign.className = "OPEN";
} else {
sign.innerHTML = "Sorry, we are CLOSE";
sign.className = "CLOSED";
window.onload = areWeOpen;
<h2>Welcome to our Store</h2>
<h3 id="openSign"></h3>
... rest of page ...

Philip M
01-10-2012, 09:14 AM
var hour = ( new Date() ).getHours;

Should be: var hour = new Date().getHours();

Old Pedant
01-10-2012, 09:43 AM
Either one works, Philip.

In general, extra parentheses never hurt when they enclose a single unit of any expression.

For example, you can write

var a = b * c + d;

but you can also write

var a = ( ( (b) * (c) ) + (d) );

I tend to use the extra parentheses in this kind of expression simply because, to me, they make the intent of the expression clearer. That is, I emphasize that you are creating an object, obtaining a reference to that object [which is designated by the parens you don't like], and only then applying a method to that object reference.

Philip M
01-10-2012, 10:12 AM
Not a case of extra parentheses. Missing parentheses. ;)


Old Pedant
01-10-2012, 10:16 AM
LOL! OH MAN! Blind as a bat I am.

Whack me upside the head.


Philip M
01-10-2012, 10:33 AM
Consider yourself duly whacked. I am diffident about making this rather impertinent suggestion, but have you considered the radical idea of actually testing the code before posting it? :rolleyes: I always do - because I am very prone to making typing errors, especially docuemnt which I often manage to get wrong.