Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with small code plz

    Hey all, I have this code on my site that simply says if we are open or not.

    Code:
    <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")}
    
    else
    { * *document.write("We are currently : Closed")}
    //-->
    </script>


    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.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,084
    Thanks
    80
    Thanked 4,552 Times in 4,516 Posts
    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:
    Code:
    <html>
    <head>
    <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;
    }
    </style>
    <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;
    </script>
    </head>
    <body>
    <center>
    <h2>Welcome to our Store</h2>
    <br/>
    <h3 id="openSign"></h3>
    <br/>
    </center>
    ... rest of page ...
    </body>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,239
    Thanks
    203
    Thanked 2,554 Times in 2,532 Posts
    var hour = ( new Date() ).getHours;

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

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,084
    Thanks
    80
    Thanked 4,552 Times in 4,516 Posts
    Either one works, Philip.

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

    For example, you can write
    Code:
    var a = b * c + d;
    but you can also write
    Code:
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,239
    Thanks
    203
    Thanked 2,554 Times in 2,532 Posts
    Not a case of extra parentheses. Missing parentheses.

    getHours()

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    Old Pedant (01-10-2012)

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,084
    Thanks
    80
    Thanked 4,552 Times in 4,516 Posts
    LOL! OH MAN! Blind as a bat I am.

    Whack me upside the head.

    THANK YOU!
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,239
    Thanks
    203
    Thanked 2,554 Times in 2,532 Posts
    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? I always do - because I am very prone to making typing errors, especially docuemnt which I often manage to get wrong.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •