HELP - "On This Day" javascript code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marcus Sheen [UK]

    HELP - "On This Day" javascript code

    Have done searches for similar questions, but cannot find anything. Nor can
    I find any resources via Google (Javascript Source etc).

    Thinking about implementing a topical "On This Day" feature on my site,
    where there would be a paragraph produced by Javascript to say:

    1995: Something notable happened
    2001: Something else of note happened

    I found something similar on http://www.freesticky.com, but it isn't really
    versatile enough for my liking as you have to put cumbersome credits on it
    etc.

    I'm not particularly Javascript-savvy, but realise that the code should:

    - Check date
    - Look at list of dates/events*
    - Output the (earlier mentioned) text

    * If possible, have the possibility of having two events for the same date,
    although this is not life-threatening.

    Please can you help? Remember - be gentle, I don't know much.

    Thanks,
    Marcus


  • Lasse Reichstein Nielsen

    #2
    Re: HELP - "On This Day" javascript code

    "Marcus Sheen [UK]" <no@email.pleas e> writes:
    [color=blue]
    > Have done searches for similar questions, but cannot find anything. Nor can
    > I find any resources via Google (Javascript Source etc).
    >
    > Thinking about implementing a topical "On This Day" feature on my site,
    > where there would be a paragraph produced by Javascript to say:
    >
    > 1995: Something notable happened
    > 2001: Something else of note happened
    >
    > I found something similar on http://www.freesticky.com, but it isn't really
    > versatile enough for my liking as you have to put cumbersome credits on it
    > etc.[/color]

    Since you don't give a reference to the script itself, and I won't go
    searching an unknown site for an unknown script, you will have to tell
    me what features you want (i.e., why it isn't versatile enough)>
    [color=blue]
    > I'm not particularly Javascript-savvy, but realise that the code should:
    >
    > - Check date
    > - Look at list of dates/events*
    > - Output the (earlier mentioned) text
    >
    > * If possible, have the possibility of having two events for the same date,
    > although this is not life-threatening.[/color]

    Easy.

    <script type="text/javascript">
    /** Place this script where you want the text in the document body **/
    /* check data */
    var now = new Date();
    var today = (now.getMonth() +1)+"/"+now.getDate() ;

    /* look at list of dates/events */
    /* list: */
    var eventList = [
    ["7/4","Independenc e Day in the US"],
    ["3/28","Someone' s birthday"],
    ["11/9","The day this script was written"]
    ];

    /* look through ... */
    for (var i=0;i<eventList .length;i++) {
    if (eventList[i][0] == today) {
    /* ... and output */
    document.write( "<p>TODAY: "+eventList[i][1]+"<\/p>");
    }
    }
    </script>

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Dr John Stockton

      #3
      Re: HELP - &quot;On This Day&quot; javascript code

      JRS: In article <islto2xk.fsf@h otpop.com>, seen in
      news:comp.lang. javascript, Lasse Reichstein Nielsen <lrn@hotpop.com >
      posted at Sun, 9 Nov 2003 18:40:55 :-[color=blue]
      >"Marcus Sheen [UK]" <no@email.pleas e> writes:[/color]
      [color=blue][color=green]
      >> Thinking about implementing a topical "On This Day" feature on my site,
      >> where there would be a paragraph produced by Javascript to say:[/color][/color]
      [color=blue]
      > var now = new Date();
      > var today = (now.getMonth() +1)+"/"+now.getDate() ;[/color]

      The separator should be "-", not "/", to imply compliance with ISO-
      8601's Y-M-D rather than the USA's M/D/Y <G>.

      [color=blue]
      > var eventList = [
      > ["7/4","Independenc e Day in the US"],
      > ["3/28","Someone' s birthday"],
      > ["11/9","The day this script was written"]
      > ];[/color]

      AIUI, events occur only once; so the year will be of interest, birthdays
      will not, but birthdates will.



      One could use

      var eventList = [
      "1776-07-04 Independence Day in the US",

      RE = new RegExp(today) // after creating today as \\d+-##-##

      if ( RE.test(eventLi st[i]) ) ...



      A more efficiently-accessed data structure might be indicated by

      var List = {
      D0704: ["1776 US Independence", "1998 Fred born"],
      D0423: ["1564 Birth", "1616 Death"] }

      today = "D0423"

      if (T=List[today]) alert(T.join('\ n'))

      --
      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
      <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
      <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: HELP - &quot;On This Day&quot; javascript code

        Dr John Stockton wrote:
        [color=blue]
        > Lasse Reichstein Nielsen [wrote]:[color=green]
        >>"Marcus Sheen [UK]" <no@email.pleas e> writes:
        >> var eventList = [
        >> ["7/4","Independenc e Day in the US"],
        >> ["3/28","Someone' s birthday"],
        >> ["11/9","The day this script was written"]
        >> ];[/color]
        >
        > AIUI, events occur only once; so the year will be of interest, birthdays
        > will not, but birthdates will.
        >
        >
        >
        > One could use
        >
        > var eventList = [
        > "1776-07-04 Independence Day in the US",
        >
        > RE = new RegExp(today) // after creating today as \\d+-##-##
        >
        > if ( RE.test(eventLi st[i]) ) ...
        >
        >
        >
        > A more efficiently-accessed data structure might be indicated by
        >
        > var List = {
        > D0704: ["1776 US Independence", "1998 Fred born"],
        > D0423: ["1564 Birth", "1616 Death"] }
        >
        > today = "D0423"
        >
        > if (T=List[today]) alert(T.join('\ n'))[/color]

        There is not need for the RegExp (untested):

        function EventList_getEv entsFor(d)
        {
        return this[d.setYear(1970)]:
        }

        function EventList()
        {
        if (typeof arguments == "undefined" )
        var arguments = EventList.argum ents;

        for (var i = 0; i < arguments.lengt h; i++)
        {
        this[Date.UTC(1970, arguments[i][0], arguments[i][1])] =
        arguments[i][2];
        }

        this.getEventsF or = EventList_getEv entsFor;
        }

        var eventList =
        new EventList(
        [1, 1, [[1616, "John Doe (1564-1616) died in Foobar"]]],
        [3, 23, [[1564, "John Doe (1564-1616) born in Foobar"]]],
        [6, 4, [[1776, "Independen ce Day (USA)"]]]);

        var e = eventList.getEv entsFor(new Date());
        if (e)
        {
        for (var i = 0; i < e.length; i++)
        {
        document.write( e[i][0] + " " + e[i][1] + "\n");
        }
        }

        But that is a feature best to be implemented server-side.


        PointedEars

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: HELP - &quot;On This Day&quot; javascript code

          Thomas 'Ingrid' Lahn wrote:
          [color=blue]
          > function EventList_getEv entsFor(d)
          > {
          > return this[d.setYear(1970)]:[/color]
          ^
          Must be a `;'.


          PointedEars

          Comment

          • Dr John Stockton

            #6
            Re: HELP - &quot;On This Day&quot; javascript code

            JRS: In article <bpbl55$1m0095$ 1@ID-107532.news.uni-berlin.de>, seen in
            news:comp.lang. javascript, Thomas 'PointedEars' Lahn
            <PointedEars@we b.de> posted at Tue, 18 Nov 2003 00:22:51 :-[color=blue]
            >Dr John Stockton wrote:
            >[color=green]
            >> Lasse Reichstein Nielsen [wrote]:[color=darkred]
            >>>"Marcus Sheen [UK]" <no@email.pleas e> writes:
            >>> var eventList = [
            >>> ["7/4","Independenc e Day in the US"],
            >>> ["3/28","Someone' s birthday"],
            >>> ["11/9","The day this script was written"]
            >>> ];[/color]
            >>
            >> AIUI, events occur only once; so the year will be of interest, birthdays
            >> will not, but birthdates will.
            >>
            >>
            >>
            >> One could use
            >>
            >> var eventList = [
            >> "1776-07-04 Independence Day in the US",
            >>
            >> RE = new RegExp(today) // after creating today as \\d+-##-##
            >>
            >> if ( RE.test(eventLi st[i]) ) ...
            >>
            >>
            >>
            >> A more efficiently-accessed data structure might be indicated by
            >>
            >> var List = {
            >> D0704: ["1776 US Independence", "1998 Fred born"],
            >> D0423: ["1564 Birth", "1616 Death"] }
            >>
            >> today = "D0423"
            >>
            >> if (T=List[today]) alert(T.join('\ n'))[/color]
            >
            >There is not need for the RegExp (untested):[/color]

            Of course it is not necessary to use a RegExp for this; the second part
            of the above does not do so. The required data is accessed directly,
            with only an implicit search.

            Untested code is generally non-functional code.

            [color=blue]
            > function EventList_getEv entsFor(d)
            > {
            > return this[d.setYear(1970)]:
            > }[/color]

            An interesting example of a disadvantage of adding [semi-] colons where
            not necessary.

            [color=blue]
            > function EventList()
            > {
            > if (typeof arguments == "undefined" )
            > var arguments = EventList.argum ents;
            >
            > for (var i = 0; i < arguments.lengt h; i++)
            > {
            > this[Date.UTC(1970, arguments[i][0], arguments[i][1])] =
            > arguments[i][2];
            > }
            >
            > this.getEventsF or = EventList_getEv entsFor;
            > }
            >
            > var eventList =
            > new EventList(
            > [1, 1, [[1616, "John Doe (1564-1616) died in Foobar"]]],
            > [3, 23, [[1564, "John Doe (1564-1616) born in Foobar"]]],
            > [6, 4, [[1776, "Independen ce Day (USA)"]]]);
            >
            > var e = eventList.getEv entsFor(new Date());
            > if (e)
            > {
            > for (var i = 0; i < e.length; i++)
            > {
            > document.write( e[i][0] + " " + e[i][1] + "\n");
            > }
            > }
            >[/color]

            It is not a good idea to enter dates with months 0..11; the risk of
            human error is too great.

            [color=blue]
            >But that is a feature best to be implemented server-side.[/color]

            When server-side is available. It is not so for some web sites; and
            client-side means that the page can be downloaded and used off-line.

            It's not clear to me what your code is supposed to do; for me, with
            colon replaced, it does nothing.

            This is fundamentally a pattern-matching or lookup job; it has little to
            do with the properties of dates, after the appropriate month and day are
            found. There is no need to use any Date Objects after that.

            --
            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
            <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
            <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
            <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: HELP - &quot;On This Day&quot; javascript code

              Dr John Stockton wrote:
              [color=blue]
              > JRS: In article <bpbl55$1m0095$ 1@ID-107532.news.uni-berlin.de>, seen in
              > news:comp.lang. javascript, Thomas 'PointedEars' Lahn
              > <PointedEars@we b.de> posted at Tue, 18 Nov 2003 00:22:51 :-[color=green]
              >>Dr John Stockton wrote:[color=darkred]
              >>> [...]
              >>> A more efficiently-accessed data structure might be indicated by
              >>>
              >>> var List = {
              >>> D0704: ["1776 US Independence", "1998 Fred born"],
              >>> D0423: ["1564 Birth", "1616 Death"] }
              >>>
              >>> today = "D0423"
              >>>
              >>> if (T=List[today]) alert(T.join('\ n'))[/color]
              >>
              >>There is not need for the RegExp (untested):[/color]
              >
              > Of course it is not necessary to use a RegExp for this; the second part
              > of the above does not do so.[/color]

              ACK but you need to encode the date you want to access somehow
              (here: "D0423") in order to access the respective property.
              [color=blue]
              > Untested code is generally non-functional code.[/color]

              NAK
              [color=blue][color=green]
              >> function EventList_getEv entsFor(d)
              >> {
              >> return this[d.setYear(1970)]:
              >> }[/color]
              >
              > An interesting example of a disadvantage of adding [semi-] colons where
              > not necessary.[/color]

              That is only a typo and I have corrected myself immediately after posted it,
              spellflamer!
              [color=blue]
              > It is not a good idea to enter dates with months 0..11; the risk of
              > human error is too great.[/color]

              OK, that can be changed.
              [color=blue][color=green]
              >>But that is a feature best to be implemented server-side.[/color]
              >
              > When server-side is available. It is not so for some web sites; and
              > client-side means that the page can be downloaded and used off-line.[/color]

              True. Nevertheless the feature is not available without client-side
              support for JavaScript for which the probability is greater here. We
              are still talking about a *web* *site*.
              [color=blue]
              > It's not clear to me what your code is supposed to do; for me, with
              > colon replaced, it does nothing.[/color]

              Debug it. It does something (the lookup) but I used the current date as
              lookup argument (for the constructor function) and there are no events
              assigned to today[tm], so there is no output for today[tm].
              [color=blue]
              > This is fundamentally a pattern-matching or lookup job; it has little to
              > do with the properties of dates, after the appropriate month and day are
              > found. There is no need to use any Date Objects after that.[/color]

              It is still a lookup job but the key, i.e. the property of the object, is
              now numeric (exactly the number of milliseconds from January 1st, 1970
              midnight in UTC) which makes it easier and more effective to pass the
              appropriate value for the lookup -- no need for further date encoding but
              *plain* Date objects can be used.


              PointedEars

              Comment

              • Dr John Stockton

                #8
                Re: HELP - &quot;On This Day&quot; javascript code

                JRS: In article <bpdsvu$1nia3h$ 1@ID-107532.news.uni-berlin.de>, seen in
                news:comp.lang. javascript, Thomas 'PointedEars' Lahn
                <PointedEars@we b.de> posted at Tue, 18 Nov 2003 20:48:27 :-[color=blue]
                >Dr John Stockton wrote:
                >[color=green]
                >> JRS: In article <bpbl55$1m0095$ 1@ID-107532.news.uni-berlin.de>, seen in
                >> news:comp.lang. javascript, Thomas 'PointedEars' Lahn
                >> <PointedEars@we b.de> posted at Tue, 18 Nov 2003 00:22:51 :-[color=darkred]
                >>>Dr John Stockton wrote:
                >>>> [...]
                >>>> A more efficiently-accessed data structure might be indicated by
                >>>>
                >>>> var List = {
                >>>> D0704: ["1776 US Independence", "1998 Fred born"],
                >>>> D0423: ["1564 Birth", "1616 Death"] }
                >>>>
                >>>> today = "D0423"
                >>>>
                >>>> if (T=List[today]) alert(T.join('\ n'))
                >>>
                >>>There is not need for the RegExp (untested):[/color]
                >>
                >> Of course it is not necessary to use a RegExp for this; the second part
                >> of the above does not do so.[/color]
                >
                >ACK but you need to encode the date you want to access somehow
                >(here: "D0423") in order to access the respective property.[/color]

                Yes, but D0423 is a rather simple coding, needed only once and easily
                read. D so that it does not start with a digit, then MM & DD. Leading
                zero on month is not essential, but there must be some way to decide
                whether 112 is Jan 12 or Nov 2.

                [color=blue][color=green]
                >> It's not clear to me what your code is supposed to do; for me, with
                >> colon replaced, it does nothing.[/color]
                >
                >Debug it. It does something (the lookup) but I used the current date as
                >lookup argument (for the constructor function) and there are no events
                >assigned to today[tm], so there is no output for today[tm].[/color]

                Yes, I allowed for that.

                Perhaps your code needs Feature Detection to deal with browsers other
                than that/those you tested with.

                BTW, 04-23 applies to someone more famous than John Doe; and I don't
                mean St. George.

                --
                © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
                <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
                <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

                Comment

                • Thomas 'PointedEars' Lahn

                  #9
                  Re: HELP - &quot;On This Day&quot; javascript code

                  Dr John Stockton wrote:
                  [color=blue]
                  > JRS: In article <bpdsvu$1nia3h$ 1@ID-107532.news.uni-berlin.de>, seen in
                  > news:comp.lang. javascript, Thomas 'PointedEars' Lahn <PointedEars@we b.de>
                  > posted at Tue, 18 Nov 2003 20:48:27 :-[/color]

                  Could you shorten that, please?
                  [color=blue][color=green]
                  >> Dr John Stockton wrote:[color=darkred]
                  >>> Of course it is not necessary to use a RegExp for this; the second
                  >>> part of the above does not do so.[/color]
                  >>
                  >> ACK but you need to encode the date you want to access somehow (here:
                  >> "D0423") in order to access the respective property.[/color]
                  >
                  > Yes, but D0423 is a rather simple coding, needed only once and easily
                  > read. D so that it does not start with a digit,[/color]

                  Although there is this restriction for JavaScript identifiers, I found it
                  useful that it does not apply to properties when referenced via the index
                  operator. That is why it is, or at least seems to be, possible to use the
                  Date serial number for their name and have the lookup based on the Date
                  object itself.
                  [color=blue]
                  > then MM & DD. Leading zero on month is not essential, but there must be
                  > some way to decide whether 112 is Jan 12 or Nov 2.[/color]

                  I agree but note that the OP asked for an "On This Day" script which reads
                  to me like an *automated* script. Sure you can create the above code using
                  the Date object _and_ string operations but it is more efficient to use the
                  Date (object), i.e. the serial number stored for it internally, itself.
                  [color=blue]
                  > Perhaps your code needs Feature Detection to deal with browsers other
                  > than that/those you tested with.[/color]

                  Perhaps. Do you get any script errors? With what UA(s) were/are you testing?
                  [color=blue]
                  > BTW, 04-23 applies to someone more famous than John Doe; and I don't mean
                  > St. George.[/color]

                  Forgive my ignorance, I have no out-of-the-box-idea who could be meant here.


                  PointedEars

                  Comment

                  • Dr John Stockton

                    #10
                    Re: HELP - &quot;On This Day&quot; javascript code

                    JRS: In article <bpj08s$1pmhf0$ 8@ID-107532.news.uni-berlin.de>, seen in
                    news:comp.lang. javascript, Thomas 'PointedEars' Lahn
                    <PointedEars@we b.de> posted at Thu, 20 Nov 2003 16:22:14 :-[color=blue]
                    >Dr John Stockton wrote:
                    >[color=green]
                    >> JRS: In article <bpdsvu$1nia3h$ 1@ID-107532.news.uni-berlin.de>, seen in
                    >> news:comp.lang. javascript, Thomas 'PointedEars' Lahn <PointedEars@we b.de>
                    >> posted at Tue, 18 Nov 2003 20:48:27 :-[/color]
                    >
                    >Could you shorten that, please?[/color]

                    I have the technology but not the inclination.
                    [color=blue][color=green]
                    >> Yes, but D0423 is a rather simple coding, needed only once and easily
                    >> read. D so that it does not start with a digit,[/color]
                    >
                    >Although there is this restriction for JavaScript identifiers, I found it
                    >useful that it does not apply to properties when referenced via the index
                    >operator. That is why it is, or at least seems to be, possible to use the
                    >Date serial number for their name and have the lookup based on the Date
                    >object itself.[/color]

                    Agreed; one would be looking up a sparse array with at most 366 elements
                    in the range 0 to 31536000000.
                    [color=blue][color=green]
                    >> then MM & DD. Leading zero on month is not essential, but there must be
                    >> some way to decide whether 112 is Jan 12 or Nov 2.[/color]
                    >
                    >I agree but note that the OP asked for an "On This Day" script which reads
                    >to me like an *automated* script. Sure you can create the above code using
                    >the Date object _and_ string operations but it is more efficient to use the
                    >Date (object), i.e. the serial number stored for it internally, itself.[/color]

                    ISTM better to use a Date Object for the probably-few dates asked about
                    than for the probably-many listed. Also that, in practice, the
                    difference will be small; but I prefer to be able to scan the data
                    readable in the source than a processed form.
                    [color=blue][color=green]
                    >> Perhaps your code needs Feature Detection to deal with browsers other
                    >> than that/those you tested with.[/color]
                    >
                    >Perhaps. Do you get any script errors? With what UA(s) were/are you testing?[/color]

                    No errors; the code appears to do nothing at all.

                    I suspect that the code, in the form in which it was posted to News, had
                    not been tested, except perhaps at midnight. The colon error supports
                    this; moreover, if I change the first function to either one of

                    function EventList_getEv entsFor(d) {
                    return this[new Date(d.setYear( 1970)).setHours (0,0,0,0)] }

                    function EventList_getEv entsFor(d) {
                    return this[Date.UTC(1970, d.getMonth(), d.getDate())] }


                    and alter one of the events (for testing today) to [10, 20, [[1564, ...
                    then it does work.

                    My view is that, before posting to News, there is no need to test code
                    of more than about a line in length in order to see whether it works;
                    one can be reasonably certain that the code, if untested, does not work.
                    If the code is tested, however, it is more likely to work first time.


                    [color=blue][color=green]
                    >> BTW, 04-23 applies to someone more famous than John Doe; and I don't mean
                    >> St. George.[/color]
                    >
                    >Forgive my ignorance, I have no out-of-the-box-idea who could be meant here.[/color]

                    "There is a history in all men's lives" --- but this man is notable not
                    only for his histories. "Cudgel thy brains no more about it"; 'twas the
                    Bard of Avon.

                    --
                    © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                    <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
                    <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
                    <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

                    Comment

                    Working...