JavaScript to change CSS according to time

Jump to First Reply
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.
     0Likes 0Dislikes 0Informatives
  • 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.
     0Likes 0Dislikes 0Informatives
  • Reply 3 of 11
    nebagakidnebagakid Posts: 2,692member
    i want to do it by time



    can i do this?
     0Likes 0Dislikes 0Informatives
  • 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>



     0Likes 0Dislikes 0Informatives
  • 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.



     0Likes 0Dislikes 0Informatives
  • 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
     0Likes 0Dislikes 0Informatives
  • Reply 7 of 11
    nebagakidnebagakid Posts: 2,692member
    you BET I am



    THankles
     0Likes 0Dislikes 0Informatives
  • 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
     0Likes 0Dislikes 0Informatives
  • 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
     0Likes 0Dislikes 0Informatives
  • 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.
     0Likes 0Dislikes 0Informatives
  • 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!!!!
     0Likes 0Dislikes 0Informatives
Sign In or Register to comment.