JavaScript to change CSS according to time

Posted:
in Mac Software edited January 2014
Can someone help me with a javascript to change the CSS according to the time of the day. So, when it is the night, the CSS would be one file. During dawn, another



Can anyone help me there?

Comments

  • Reply 1 of 11
    Does your host support PHP? If so this would be a real quick script I could do up for you.
  • Reply 2 of 11
    Poke around Apple's iPod site... They are using an onclick function to change the CSS, so just modify this behaviour to get and check the time. An if-else should do fine.
  • Reply 3 of 11
    nebagakidnebagakid Posts: 2,692member
    i want to do it by time



    can i do this?
  • Reply 4 of 11
    nebagakidnebagakid Posts: 2,692member
    This is what I have, but it no workee!

    Quote:

    <script type="text/javascript">



    dateObject = new Date();

    theTime = dateObject.getHours();



    if (theTime < 8 )

    {

    document.write("<link rel='stylesheet' type='text/css' href='styles-afternoon.css'>");

    }



    else if ((theTime > 8 ) && (theTime < 17))



    {

    document.write("<link rel='stylesheet' type='text/css' href='styles-night.css'>");

    }

    else if ((theTime > 17) && (theTime < 21))



    {___

    document.write("<link rel='stylesheet' type='text/css' href='styles-afternoon.css'>");

    }



    else



    {

    document.write("<link rel='stylesheet' type='text/css' href='styles-night.css'>");

    }

    </script>



  • Reply 5 of 11
    Quote:

    Originally posted by Mount_my_floppy

    Does your host support PHP? If so this would be a real quick script I could do up for you.



  • Reply 6 of 11
    Sorry.. Just noticed that it was you asking for help and not someone posting a solution.



    You just missed out 'var' in front of 'dateObject' and 'theTime'.

    The code should look like that:

    Quote:

    <script type="text/javascript">



    var dateObject = new Date();

    var theTime = dateObject.getHours();



    ...



    Your if-else seems to contain quite a bit of unnecessary switches... But I guess you're going to do morning and stuff as well
  • Reply 7 of 11
    nebagakidnebagakid Posts: 2,692member
    you BET I am



    THankles
  • Reply 8 of 11
    nebagakidnebagakid Posts: 2,692member
    Quote:

    Originally posted by Mount_my_floppy



    i am doing this just on my computer here, no PHP
  • Reply 9 of 11
    Quote:

    Originally posted by Nebagakid

    i am doing this just on my computer here, no PHP



    Could have told the fella earlier...



    Also, please stop smoking. It's bad for you. :P
  • Reply 10 of 11
    And the code needs to be commented out so that it will not display on browsers with no scripting capabilities. You will also need a <noscript> section.
  • Reply 11 of 11
    nebagakidnebagakid Posts: 2,692member
    Quote:

    Originally posted by catalyst

    Could have told the fella earlier...



    Also, please stop smoking. It's bad for you. :P




    oddly enough i don't smoke!!!!
Sign In or Register to comment.