what's wrong with this...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ross Levis

    #1

    what's wrong with this...

    I wrote some javascript code a couple of years ago to load a web page based
    on the current date & time. I just modified it today to change the naming
    format of the web pages and I can't get it to work at all. Even the old one
    which was working (on Win98) is not working at all on my WinXP Pro. Has
    some new requirement been introduced?

    Can anyone see what's wrong with the following code. If it is 9am on July
    12, it should load a web page named: Jul12-09.html, but it does nothing at
    all.

    <html>
    <head>
    <script language="JavaS cript">
    <!--
    function lp() {
    ms=new Array(11);
    ms[0]="Jan";
    ms[1]="Feb";
    ms[2]="Mar";
    ms[3]="Apr";
    ms[4]="May";
    ms[5]="Jun";
    ms[6]="Jul";
    ms[7]="Aug";
    ms[8]="Sep";
    ms[9]="Oct";
    ms[10]="Nov";
    ms[11]="Dec";
    dt=new Date();
    m=dt.getMonth() ;
    d=dt.getDate();
    d=(d<10)?"0"+d: d;
    h=dt.getHours() ;
    h=(h<10)?"0"+h: h;
    location.href=m s[m]+d+"-"+h+".html" ;
    }
    //-->
    </script>
    </head>
    <body onload="lp()"></body>
    </html>

    Thanks,
    Ross Levis.


  • Evertjan.

    #2
    Re: what's wrong with this...

    Ross Levis wrote on 12 jul 2003 in comp.lang.javas cript:
    [color=blue]
    > I wrote some javascript code a couple of years ago to load a web page
    > based on the current date & time. I just modified it today to change
    > the naming format of the web pages and I can't get it to work at all.
    > Even the old one which was working (on Win98) is not working at all on
    > my WinXP Pro. Has some new requirement been introduced?
    >
    > Can anyone see what's wrong with the following code. If it is 9am on
    > July 12, it should load a web page named: Jul12-09.html, but it does
    > nothing at all.
    >
    > <html>
    > <head>
    > <script language="JavaS cript">
    > <!--
    > function lp() {
    > ms=new Array(11);
    > ms[0]="Jan";
    > ms[1]="Feb";
    > ms[2]="Mar";
    > ms[3]="Apr";
    > ms[4]="May";
    > ms[5]="Jun";
    > ms[6]="Jul";
    > ms[7]="Aug";
    > ms[8]="Sep";
    > ms[9]="Oct";
    > ms[10]="Nov";
    > ms[11]="Dec";
    > dt=new Date();
    > m=dt.getMonth() ;
    > d=dt.getDate();
    > d=(d<10)?"0"+d: d;
    > h=dt.getHours() ;
    > h=(h<10)?"0"+h: h;
    > location.href=m s[m]+d+"-"+h+".html" ;
    >}
    > //-->
    > </script>
    > </head>
    > <body onload="lp()"></body>
    > </html>[/color]

    Please explain "not working", as this works:

    <script language="JavaS cript">
    <!--
    // function lp() {
    ms=new Array(11);
    ms[0]="Jan";
    ms[1]="Feb";
    ms[2]="Mar";
    ms[3]="Apr";
    ms[4]="May";
    ms[5]="Jun";
    ms[6]="Jul";
    ms[7]="Aug";
    ms[8]="Sep";
    ms[9]="Oct";
    ms[10]="Nov";
    ms[11]="Dec";
    dt=new Date();
    m=dt.getMonth() ;
    d=dt.getDate();
    d=(d<10)?"0"+d: d;
    h=dt.getHours() ;
    h=(h<10)?"0"+h: h;
    // location.href=m s[m]+d+"-"+h+".html" ;
    alert(ms[m]+d+"-"+h+".html" );
    // }
    //-->
    </script>

    Perhaps your relative location is not leading to a file.

    Local time here still gives:

    Jul12-08.html

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)

    Comment

    • Evertjan.

      #3
      Re: what's wrong with this...

      DU wrote on 12 jul 2003 in comp.lang.javas cript:[color=blue][color=green]
      >> ms=new Array(11);[/color]
      >
      > Your script is requesting to create an array for 11 items, not 12.[/color]

      Good for you, I didn't see that!
      [color=blue]
      > I recommend explicitly declaring each local variables
      > with the var keyword.[/color]

      why ?
      [color=blue][color=green]
      >> DU wrote:
      >> Your script is requesting to create an array for 11 items, not 12.[/color]
      > I wondered about that. I tried 12 but no difference.[/color]

      Test it again in December ;-)

      btw: cann't you do this with serverside redirecting ?

      --
      Evertjan.
      The Netherlands.
      (Please change the x'es to dots in my emailaddress)

      Comment

      • Hywel Jenkins

        #4
        Re: what's wrong with this...

        In article <FcNPa.77174$JA 5.1479735@news. xtra.co.nz>,
        ross@stationpla ylist.dot.com says...[color=blue]
        > I wrote some javascript code a couple of years ago to load a web page based
        > on the current date & time. I just modified it today to change the naming
        > format of the web pages and I can't get it to work at all. Even the old one
        > which was working (on Win98) is not working at all on my WinXP Pro. Has
        > some new requirement been introduced?
        >
        > Can anyone see what's wrong with the following code. If it is 9am on July
        > 12, it should load a web page named: Jul12-09.html, but it does nothing at
        > all.[/color]

        Works for me in IE6 and Moz1.something under XP Pro.

        --
        Hywel Never knowingly understood


        Comment

        • DU

          #5
          Re: what's wrong with this...

          Ross Levis wrote:[color=blue]
          > DU wrote:
          >[color=green]
          >>Your script is requesting to create an array for 11 items, not 12.[/color]
          >
          >
          > I wondered about that. I tried 12 but no difference.
          >
          >[color=green]
          >>I recommend explicitly declaring each local variables with the var[/color]
          >
          > keyword.
          >
          > OK, but that should not stop it working should it?
          >[/color]

          It's just good and sound coding techniques. Same thing with using a
          doctype declaration and making sure that the markup is valid to begin
          with. FYI, even MSDN kinda suggest this when script problems appear.
          [color=blue]
          > Ross.
          >
          >[/color]

          When you say "not working", please be specific, give details, describe,
          paste the javascript console error message(s) if any, define the
          browsers and browser versions that do not work, etc... As far as I can
          see, this should work and I tested the code (not the same as given) and
          it worked in MSIE 6 for Windows (standards compliant rendering mode) and
          with NS 7.1.

          DU
          --
          Javascript and Browser bugs:


          Comment

          • Dr John Stockton

            #6
            Re: what's wrong with this...

            JRS: In article <FcNPa.77174$JA 5.1479735@news. xtra.co.nz>, seen in
            news:comp.lang. javascript, Ross Levis <ross@stationpl aylist.dot.com>
            posted at Sat, 12 Jul 2003 17:41:56 :-
            [color=blue]
            > ms=new Array(11);
            > ms[0]="Jan";
            > ms[1]="Feb";
            > ms[2]="Mar";
            > ms[3]="Apr";
            > ms[4]="May";
            > ms[5]="Jun";
            > ms[6]="Jul";
            > ms[7]="Aug";
            > ms[8]="Sep";
            > ms[9]="Oct";
            > ms[10]="Nov";
            > ms[11]="Dec";[/color]

            Your code fails appropriately in my system, complaining about the
            absence of the page Jul12-18.html.

            ms = ["Jan", "Feb", ..., "Dec"] // saves space, and counting.

            --
            © 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

            • Evertjan.

              #7
              Re: what's wrong with this...

              Dr John Stockton wrote on 12 jul 2003 in comp.lang.javas cript:
              [color=blue]
              > JRS: In article <FcNPa.77174$JA 5.1479735@news. xtra.co.nz>, seen in
              > news:comp.lang. javascript, Ross Levis <ross@stationpl aylist.dot.com>
              > posted at Sat, 12 Jul 2003 17:41:56 :-
              >[color=green]
              >> ms=new Array(11);
              >> ms[0]="Jan";
              >> ms[1]="Feb";
              >> ms[2]="Mar";
              >> ms[3]="Apr";
              >> ms[4]="May";
              >> ms[5]="Jun";
              >> ms[6]="Jul";
              >> ms[7]="Aug";
              >> ms[8]="Sep";
              >> ms[9]="Oct";
              >> ms[10]="Nov";
              >> ms[11]="Dec";[/color]
              >
              > Your code fails appropriately in my system, complaining about the
              > absence of the page Jul12-18.html.
              >
              > ms = ["Jan", "Feb", ..., "Dec"] // saves space, and counting.[/color]

              an alternate way saves on counting only:

              ms=new Array(12);
              // 12 !!
              n = 0;
              ms[n++]="Jan";
              ms[n++]="Feb";
              ms[n++]="Mar";
              ms[n++]="Apr";
              ms[n++]="May";
              ms[n++]="Jun";
              // these rows can easily be sorted alphabetically in an editor,
              // not usefull in this months case of course.


              --
              Evertjan.
              The Netherlands.
              (Please change the x'es to dots in my emailaddress)

              Comment

              • Evertjan.

                #8
                Re: what's wrong with this...

                Lasse Reichstein Nielsen wrote on 13 jul 2003 in comp.lang.javas cript:[color=blue]
                > "Evertjan." writes:[color=green]
                >> Test it again in December ;-)[/color]
                >
                > It won't matter. Javascript arrays can have their lengths changed
                > dynamically, so when it executes
                > ms[11]="Dec";[/color]

                I know that. It was partly ment as a joke. ";-)"

                But also it is good practice to test such things out
                for instance by temporariliy insertion:

                .......
                dt=new Date();
                m=dt.getMonth() ;
                m = 11; // this is the extra test line
                .......


                --
                Evertjan.
                The Netherlands.
                (Please change the x'es to dots in my emailaddress)

                Comment

                Working...