dynamic banner text

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • S.Hoitinga

    dynamic banner text

    Hi,

    I am trying to create a header on a page that switches between 7 textitems
    in an array.
    using this code:

    <script language="JavaS cript">
    season=new Array("summer", "winter","fall" ,"spring",")
    i=0
    function raise()
    {
    setTimeout('i=i +1',2000);
    if(i>4){i=0}
    }
    </script>

    </head>

    <body onLoad="raise() ">
    <script language="JavaS cript">

    document.write( season[i]);
    </script>

    can anybody please explain me why this doesnot work.

    greets,

    Sybolt


  • kaeli

    #2
    Re: dynamic banner text

    In article <aphyc.1001$v34 .817@amsnews03. chello.com>,
    s.hoitinga@chel lo.nl enlightened us with...[color=blue]
    > Hi,
    >
    > I am trying to create a header on a page that switches between 7 textitems
    > in an array.
    > using this code:
    >
    > <script language="JavaS cript">
    > season=new Array("summer", "winter","fall" ,"spring",")[/color]

    You seem to have an extra quote there.
    season=new Array("summer", "winter","fall" ,"spring");
    [color=blue]
    > i=0
    > function raise()
    > {
    > setTimeout('i=i +1',2000);
    > if(i>4){i=0}
    > }
    > </script>
    >
    > </head>
    >
    > <body onLoad="raise() ">
    > <script language="JavaS cript">
    >
    > document.write( season[i]);[/color]

    document.write tends to wipe out the rest of the document.
    I don't what you're really trying to do, but I'll assume you want it to
    switch the header ever 2 seconds.

    Try this. Tested in NN7 and IE6. Watch for word-wrap.
    This will NOT work in old browsers like NN4.

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
    "http://www.w3.org/TR/REC-html40/loose.dtd">
    <html>
    <head>
    <title> New Document </title>
    <script language="JavaS cript" type="text/javascript">
    var season=new Array("summer", "winter","fall" ,"spring");
    var i=0;

    function raise()
    {
    setTimeout('rai se()',2000);
    if(i >= season.length)
    {
    i=0;
    }
    if (document.getEl ementById)
    {
    var e = document.getEle mentById("heade r");
    var oTextNode = document.create TextNode(season[i]);
    var oReplaceNode = e.childNodes[0];
    e.replaceChild( oTextNode,oRepl aceNode);
    }
    i++;
    }
    </script>

    </head>

    <body onload="raise() ">
    <div id="header">hea der</div>
    <div id="body">body </div>
    </body>
    </html>

    --
    --
    ~kaeli~
    Any sufficiently advanced technology is indistinguishab le
    from magic.



    Comment

    • Vincent van Beveren

      #3
      Re: dynamic banner text

      Hi Sybolt,
      [color=blue]
      > can anybody please explain me why this doesnot work.
      >
      > greets,
      >
      > Sybolt[/color]

      If I understand your script corretly the idea is that it changes one
      searson into the other every 2 seconds. The script doesn't work
      for serveral reasons. If it does not give any errors it probably
      shows 'summer' and quits working.

      document.writel n() writes only a snapshot of the contence of your
      seasons[i]. So it writes to the screen what seasons[i] contains at
      the moment it is written (which is only while loading the page).
      After it has written 'summer' to the screen it will never be changed
      anymore after it was written. Its not like GUI components in a visual
      development enviroment like Visual Studio. you have to keep in mind
      that you are writing a page not building a GUI.

      The function raise is called onload, but besides changing 'i' it never
      tells the browser to update the text on the screen. To update you'll
      need to access the document object model.

      setTimeout is called once after two seconds, and increases 'i' but it
      does only that. if(i>4){i=0} is only called once (onload).


      What you need to do to get this to work:

      1. create an accessable element on the document body. For example

      <SPAN ID="seasons"></SPAN>

      2. Write a _function_ that changes the 'i' and then updates the SPAN
      using the document object model.

      mySeasons = document.getEle mentById('seaso ns');
      mySeasons.inner HTML = 'Whatever you want'

      3. Call this function every 2 seconds or so:

      window.setInter val(2000, 'myfunctionname ()');

      I hope these are enough hints to get it functioning. if you have
      questions, feel free to ask.

      Greetings,
      Vincent

      Comment

      • Richard Cornford

        #4
        Re: dynamic banner text

        S.Hoitinga wrote:[color=blue]
        > I am trying to create a header on a page that switches
        > between 7 textitems in an array.
        > using this code:
        >
        > <script language="JavaS cript">
        > season=new Array("summer", "winter","fall" ,"spring",")
        > i=0[/color]
        ^
        While it isn't necessary to declare global variables it is a good idea
        to do so anyway as it makes it clear that they are intended to be
        global.
        [color=blue]
        > function raise()
        > {
        > setTimeout('i=i +1',2000);[/color]

        setTimeout schedules the execution of code at some future point (2
        seconds time in this case) but does not hold up the execution of the
        code that calls it.
        [color=blue]
        > if(i>4){i=0}
        > }
        > </script>
        >
        > </head>
        >
        > <body onLoad="raise() ">
        > <script language="JavaS cript">
        > document.write( season[i]);[/color]

        This document write statement in executed inline as the page loads,
        the - i - value will always be zero at this point as nothing has
        happened to change that so the write function will always write out
        "summer".
        [color=blue]
        > </script>
        >
        > can anybody please explain me why this doesnot work.[/color]

        It does work, in so far as it will be doing exactly what you have
        programmed it to do. To tell you why it doesn't do what you want it to
        do it would be necessary for you to explain what that was. Your opening
        sentence of explanation doesn't achieve that as it does not appear to
        have any baring on either the javascript code used or the HTML and
        doesn't qualify critical words like "create" and "switches" in terms of
        who, how, where, when, whither, which, wherefore and why.

        Richard.


        Comment

        • Mick White

          #5
          Re: dynamic banner text

          S.Hoitinga wrote:
          [color=blue]
          > <script language="JavaS cript">
          > season=new Array("summer", "winter","fall" ,"spring",")
          > i=0
          > function raise()
          > {
          > setTimeout('i=i +1',2000);
          > if(i>4){i=0}
          > }
          > </script>[/color]

          <script type="text/JavaScript">
          var season=new Array("summer", "winter","fall" ,"spring"),i =0;
          function raise() {
          document.getEle mentById("foo") .innerHTML=seas on[i];
          i++;
          if(i>3){i=0}
          setTimeout('rai se()',2000);
          }
          </script>
          [color=blue]
          >
          > </head>
          >
          > <body onLoad="raise() ">[/color]

          <p id="foo"></p>

          [color=blue]
          > <script language="JavaS cript">
          >
          > document.write( season[i]);
          > </script>[/color]


          The script above is not required
          Mick

          Comment

          • rh

            #6
            Re: dynamic banner text

            Mick White wrote:

            <snip>
            [color=blue]
            > <script type="text/JavaScript">
            > var season=new Array("summer", "winter","fall" ,"spring"),i =0;
            > function raise() {
            > document.getEle mentById("foo") .innerHTML=seas on[i];
            > i++;
            > if(i>3){i=0}
            > setTimeout('rai se()',2000);
            > }
            > </script>
            >[/color]

            Which may also been seen as a job for the Mod squad:

            <script type="text/JavaScript">
            var season = ["summer","fall" ,"winter","spri ng"],i=0; // *re-ordered*
            function raise() {
            document.getEle mentById("foo") .innerHTML=seas on[i++ % 4];
            setTimeout('rai se()',2000);
            }
            </script>

            ../rh

            Comment

            • rh

              #7
              Re: dynamic banner text

              rh wrote:
              [color=blue]
              >Which may also been seen as a job for the Mod squad:
              >
              ><script type="text/JavaScript">
              > var season = ["summer","fall" ,"winter","spri ng"],i=0; // *re-ordered*
              > function raise() {
              > document.getEle mentById("foo") .innerHTML=seas on[i++ % 4];
              > setTimeout('rai se()',2000);
              > }
              ></script>[/color]

              Then again, that would not be a great idea for an unbounded loop,
              since "i" continues to increase in value (and could overflow). The
              update line could be written as:

              document.getEle mentById("foo") .innerHTML=seas on[i = ++i % 4];

              (with "i" initialized to -1), however.

              Apologies for the distraction. :).

              ../rh

              Comment

              • Dr John Stockton

                #8
                Re: dynamic banner text

                JRS: In article <290e65a0.04061 12002.77b41bf5@ posting.google. com>, seen
                in news:comp.lang. javascript, rh <codeHanger@yah oo.ca> posted at Fri, 11
                Jun 2004 21:02:56 :[color=blue]
                >rh wrote:
                >[color=green]
                >>Which may also been seen as a job for the Mod squad:
                >>
                >><script type="text/JavaScript">
                >> var season = ["summer","fall" ,"winter","spri ng"],i=0; // *re-ordered*
                >> function raise() {
                >> document.getEle mentById("foo") .innerHTML=seas on[i++ % 4];
                >> setTimeout('rai se()',2000);
                >> }
                >></script>[/color]
                >
                >Then again, that would not be a great idea for an unbounded loop,
                >since "i" continues to increase in value (and could overflow). The
                >update line could be written as:
                >
                > document.getEle mentById("foo") .innerHTML=seas on[i = ++i % 4];
                >
                >(with "i" initialized to -1), however.[/color]

                The variable i will increase to the value of 900719925474099 2, which
                will take 180143985094819 84 seconds; starting about now, it will be
                reached in the late morning of Monday 4th April 570855568. But it will
                not then overflow; the number will remain at that value, and it will by
                that script be perpetual Summer. Alas, the browser will probably nor
                run that long.

                --
                © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
                Web <URL:http://www.merlyn.demo n.co.uk/> - w. FAQish topics, links, acronyms
                PAS EXE etc : <URL:http://www.merlyn.demo n.co.uk/programs/> - see 00index.htm
                Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

                Comment

                • rh

                  #9
                  Re: dynamic banner text

                  Dr John Stockton <spam@merlyn.de mon.co.uk> wrote in message news:<nd7lroBNN 0yAFwOK@merlyn. demon.co.uk>...[color=blue]
                  > JRS: In article <290e65a0.04061 12002.77b41bf5@ posting.google. com>, seen
                  > in news:comp.lang. javascript, rh <codeHanger@yah oo.ca> posted at Fri, 11
                  > Jun 2004 21:02:56 :[color=green]
                  > >rh wrote:
                  > >[color=darkred]
                  > >>Which may also been seen as a job for the Mod squad:
                  > >>
                  > >><script type="text/JavaScript">
                  > >> var season = ["summer","fall" ,"winter","spri ng"],i=0; // *re-ordered*
                  > >> function raise() {
                  > >> document.getEle mentById("foo") .innerHTML=seas on[i++ % 4];
                  > >> setTimeout('rai se()',2000);
                  > >> }
                  > >></script>[/color]
                  > >
                  > >Then again, that would not be a great idea for an unbounded loop,
                  > >since "i" continues to increase in value (and could overflow). The
                  > >update line could be written as:
                  > >
                  > > document.getEle mentById("foo") .innerHTML=seas on[i = ++i % 4];
                  > >
                  > >(with "i" initialized to -1), however.[/color]
                  >
                  > The variable i will increase to the value of 900719925474099 2, which
                  > will take 180143985094819 84 seconds; starting about now, it will be
                  > reached in the late morning of Monday 4th April 570855568. But it will
                  > not then overflow; the number will remain at that value, and it will by
                  > that script be perpetual Summer. Alas, the browser will probably nor
                  > run that long.[/color]

                  Hmmm, and I thought it would occur much sooner, in 570853563 :-).

                  (I see, according to ECMAScript documentation, and the FAQ, that the
                  maximum integer is actually 2^53, and further note that since integers
                  are not -normally- represented as signed 32-bit, they do not overflow
                  into the negative range -- not that that would necessarily occur, even
                  in that case).

                  And not even if the the maximum integer was 2^31-1, as I had
                  incorrectly assumed/recollected, would there be a problem. I'll try
                  keep perceived threats in perspective.

                  ../rh

                  Comment

                  Working...