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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Nov 2006
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Expanding Message of the day script to > 31 days

    Hi all,

    I got this "Tip of the Day" script from http://javascriptkit.com :

    Code:
    <script>
    <!--
    
    /*
    Tip of the day script
    By JavaScript Kit (http://javascriptkit.com)
    Over 200+ free scripts here!
    */
    
    var today_obj=new Date()
    var today_date=today_obj.getDate()
    
    var tips=new Array()
    //Configure the below variable to contain the "header" of the tip
    var tiptitle='<img src="../../tip.gif"> <b>JavaScript Tip of the day</b><br />'
    
    //Configure the below array to hold the 31 possible tips of the month
    tips[1]='Tip 1 goes here'
    tips[2]='Tip 2 goes here'
    tips[3]='Tip 3 goes here'
    tips[4]='Tip 4 goes here'
    tips[5]='Tip 5 goes here'
    tips[6]='Tip 6 goes here'
    tips[7]='Tip 7 goes here'
    tips[8]='Tip 8 goes here'
    tips[9]='Tip 9 goes here'
    tips[10]='Tip 10 goes here'
    tips[11]='Tip 11 goes here'
    tips[12]='Tip 12 goes here'
    tips[13]='Tip 13 goes here'
    tips[14]='Tip 14 goes here'
    tips[15]='Tip 15 goes here'
    tips[16]='Tip 16 goes here'
    tips[17]='Tip 17 goes here'
    tips[18]='Tip 18 goes here'
    tips[19]='Tip 19 goes here'
    tips[20]='Tip 20 goes here'
    tips[21]='Tip 21 goes here'
    tips[22]='Tip 22 goes here'
    tips[23]='Tip 23 goes here'
    tips[24]='Tip 24 goes here'
    tips[25]='Tip 25 goes here'
    tips[26]='Tip 26 goes here'
    tips[27]='Tip 27 goes here'
    tips[28]='Tip 28 goes here'
    tips[29]='Tip 29 goes here'
    tips[30]='Tip 30 goes here'
    tips[31]='Tip 31 goes here'
    
    document.write(tiptitle)
    document.write(tips[today_date])
    
    //-->
    </script>
    While it works perfectly, I want to have more tips than 31.
    As I understand the getDate object, after it reaches to last day of the given month, it will start over at day 1 of the following month.

    I would like the script to accommodate however many "tips" I have put into the array.
    It looks like a fairly simple task, but I am apparently less than simple

    Can someone lend a hand here?

    Thanks,
    Rick

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,159
    Thanks
    203
    Thanked 2,549 Times in 2,527 Posts
    Try this:-

    Code:
    <script type = "text/javascript">
    
    Date.prototype.getDOY = function() {
    var onejan = new Date(this.getFullYear(),0,1);
    return Math.ceil((this - onejan) / 86400000);
    } 
    var today = new Date();
    var DOY = today.getDOY(); // DOY is day of year 1-365 or 366 in leap year
    
    DOY = (parseInt(DOY%5))+1;  // 5 is number of tips currently specified in the TOD array - you can have as many as you want up to 366
    
    var TOD = new Array(366);
    TOD[1] = "Drink Coca-Cola ice cold";
    TOD[2] = "Clean your teeth daily";
    TOD[3] = "Eat more fruit";
    TOD[4] = "A stitch in time saves nine";
    TOD[5] = "Marry in  haste, repent at leisure";
    
    // and so on up to TOD[366] (to allow for leap year)
    
    var weekDays = new Array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
    var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
    var dd = today.getDate();
    var dy = today.getDay();
    var mm = today.getMonth();
    var yy = today.getFullYear();
    
    var daySuffix = 'th';
    if(dd != 11 && dd != 12 && dd != 13) {ddx = dd % 10}
    else {ddx = dd}
    if (ddx == 1) { daySuffix = 'st' }
    if (ddx == 2) { daySuffix = 'nd' }
    if (ddx == 3) { daySuffix = 'rd' }
    
    var tipOfDay = "Tip of the day for " + weekDays[dy] + " " + dd  + daySuffix + " " + months[mm] + " " + yy + ":- &nbsp&nbsp  " + TOD[DOY]
    document.write(tipOfDay);
    
    </script>

    "Success is the ability to go from one failure to another with no loss of enthusiasm." - Sir Winston Churchill, British politician (1874 - 1965)
    Last edited by Philip M; 07-21-2009 at 06:00 PM. Reason: Typo

  • #3
    New to the CF scene
    Join Date
    Nov 2006
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi Philip,

    Thanks for the fast response

    I think I am still doing something incorrectly (nothing is showing up).

    Code:
    <script type = "text/javascript">
    
    Date.prototype.getDOY = function() {
    var onejan = new Date(this.getFullYear(),0,1);
    return Math.ceil((this - onejan) / 86400000);
    } 
    var today = new Date();
    var DOY = today.getDOY(); // DOY is day of year 1-365 or 366 in leap year
    
    DOY = (parseInt(DOY%43))+1;  // 5 is number of tips currently specified in the TOD array - you can have as many as you want up to 366
    
    var TOD = new Array(366);
    TOD[1] = "Lock your door, even when you intend to return home shortly or even if you are just going down the hall. It takes a thief ten seconds or less to enter an open room and steal your property";
      TOD[2] = "Lock or secure doors and windows when you are alone or asleep";
      TOD[3] = "Keep emergency numbers by your phone";
      TOD[4] = "Do not leave messages on your door indicating that you are away and when
        you will return";
      TOD[5] = "Do not let strangers enter your home";
      TOD[6] = "Do not prop open outer doors";
      TOD[7] = "If someone asks to use your phone for an emergency call, offer to telephone
        for them instead of allowing them access";
      TOD[8] = "Do not put your address on your key ring";
      TOD[9] = "Know your neighbors";
      TOD[10] = "Do not leave keys in hiding places. Thieves will find them. Carry your
        keys or make sure that anyone who truly needs them has their own copy";
      TOD[11] = "Call 911 to report suspicious persons or activity in or around your neighborhood";
      TOD[12] = "Open a savings or checking account instead of keeping money in your room";
      TOD[13] = "Keep automatic teller machine cards in a safe place, keep your PIN number
        secret. When possible, only use ATMs during the day";
      TOD[14] = "Instead of carrying large sums of cash use a charge card. Some charge cards
        insure property purchased with those cards against loss, theft or damage";
      TOD[15] = "If you find yourself in immediate danger, call 911; try to stay calm and
        get away at the first opportunity";
      TOD[16] = "Avoid walking alone at night unless absolutely necessary";
      TOD[17] = "Keep to well lit commonly traveled routes";
      TOD[18] = "Avoid shortcuts and dark, isolated areas";
      TOD[19] = "Walk purposefully, know where you are going, and project a no-nonsense
        image";
      TOD[20] = "Avoid potentially dangerous situations";
      TOD[21] = "If you feel threatened, cross the street, locate an emergency phone, or
        enter a store or place of business even if you have just left it";
      TOD[22] = " Have your door keys ready; carry them in your pockets, not buried in a
        purse";
      TOD[23] = "Always lock your car. (one in five stolen cars was left with keys in the
        ignition.)";
      TOD[24] = "Lock bikes to immovable objects or bike racks with hardened alloy locks
        and chains or U-shaped lock";
      TOD[25] = "Do not leave tempting valuables or property visible inside the car. Lock
        these items in the trunk";
      TOD[26] = "Lock mopeds as you would bikes";
      TOD[27] = "Look into your car before getting in. Lock doors and roll up windows once
        inside for protection";
      TOD[28] = "Never pick up hitchhikers";
      TOD[29] = "Carry change for emergency calls. 911 is a free call";
      TOD[30] = "Drive to a police or fire station or open place of business if you feel
        you are being followed";
      TOD[31] = "Do not stop to help occupants of stopped or disabled vehicles";
      TOD[32] = "Continue driving to the nearest phone and call assistance for them";
      TOD[33] = "Raise the hood, and then lock yourself into your car if it breaks down.
        If someone stops and offers you help, remain in your car and ask them to
        phone for help. Do not worry about seeming rude";
      TOD[34] = "Travel with a friend or in a group";
      TOD[35] = "Be alert and aware of your surroundings";
      TOD[36] = "Avoid dark, vacant or deserted areas; use well-lit, well-traveled routes";
      TOD[37] = "Dress in clothes and shoes which will not hamper movemen";
      TOD[38] = "Move away from the potential threat if possible";
      TOD[39] = "Join any group of people nearby; cross the street and increase your pace";
      TOD[41] = "If a threatening situation is imminent, and people are close by to help,
        yell, scream or make a commotion in any way you can to get their attention";
      TOD[42] = "Go to an open business";
      TOD[43] = "Call 911 from a safe location";
    
    // and so on up to TOD[366] (to allow for leap year)
    
    var weekDays = new Array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
    var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
    var dd = today.getDate();
    var dy = today.getDay();
    var mm = today.getMonth();
    var yy = today.getFullYear();
    
    var daySuffix = 'th';
    if(dd != 11 && dd != 12 && dd != 13) {ddx = dd % 10}
    else {ddx = dd}
    if (ddx == 1) { daySuffix = 'st' }
    if (ddx == 2) { daySuffix = 'nd' }
    if (ddx == 3) { daySuffix = 'rd' }
    
    var tipOfDay = "Tip of the day for " + weekDays[dy] + " " + dd  + daySuffix + " " + months[mm] + " " + yy + ":- &nbsp&nbsp  " + TOD[DOY]
    document.write(tipOfDay);
    
    </script>
    See anything obvious?
    I put DOY%43 because there is currently 43 tips )TOD) in the array.

    Also, if I wanted to place the data in a Table (for formatting), how would I call the script from an external javascript file?

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,159
    Thanks
    203
    Thanked 2,549 Times in 2,527 Posts
    You have introduced line feeds/carriage returns into many of your tips:-

    TOD[4] = "Do not leave messages on your door indicating that you are away and when
    you will return";

    You must eliminate these so (all text one one line):-

    TOD[4] = "Do not leave messages on your door indicating that you are away and when you will return";

    or if you are trying to format the output replace the LF/CR with <br> e.g.

    TOD[7] = "If someone asks to use your phone for an emergency call, <br> offer to telephone for them instead of allowing them access";

    If you put these right you should find that it works fine. Today's tip is
    "Do not stop to help occupants of stopped or disabled vehicles";

    I put DOY%43 because there is currently 43 tips (TOD) in the array. Yes, you've got it.
    Last edited by Philip M; 07-21-2009 at 09:04 PM.

  • Users who have thanked Philip M for this post:

    spycat (07-22-2009)

  • #5
    New to the CF scene
    Join Date
    Nov 2006
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hey Philip,

    Thanks so very much for the help on this.
    I sincerely appreciate it. Its great that there are people out there willing to lend a hand.

    I did a bit of formatting:
    Code:
    var tipOfDay = "<center>" + "<strong>" +"Crime Buster tip of the Day for " + weekDays[dy] + " " + dd  + daySuffix + " " + months[mm] + " " + yy + ":- &nbsp&nbsp  " + "<br><br>" + "</strong>" + "</center>" + TOD[DOY]
    document.write(tipOfDay);
    I called the javascript from within a table, which I will continue to format a bit more:
    Code:
    <table border="1" width="200px" bgcolor="#E8E8E8" cellspacing="1" cellpadding="2" style="font-size:8pt;">
    <tr><td width="200px">
    <script type="text/javascript" language="javascript" src="tip-of-day.js"></script>
    </td></tr></table>
    Again, I appreciate your assistance!

    Rick

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,159
    Thanks
    203
    Thanked 2,549 Times in 2,527 Posts
    Rick - Glad to have been able to assist. Naturally you can change the display format to suit your requirements, although it is preferable to use CSS.

    <script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead.

    TOD[44] - Never sleep 13 in a bed on a Friday.


  •  

    Tags for this Thread

    Posting Permissions

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