passing variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Judy M

    passing variables

    I am a Javascript Newbie, but have read two of the brick-sized books
    and read the deja section and FAQ's and didn't find my answer. I'm
    sure it is simple and apologize in advance, but here goes...

    I want to pass a global variable. The code I have looks functionally
    like this:
    <head>
    <Script...>
    mypix= (this is an array of file names. This part works fine)
    var thispic=0
    var imgct =mypix.length -1
    </script>

    function.proces snext() {
    if document.images && thispic < imgct {
    thispic++
    document.mypict ure.src=mypix[thispic]
    }
    </head>
    <body>
    a href="javascrip t.processnext() "> Next &gt; &gt;</a>
    <script.languag e="javascript " type="text.java script"
    document.write( thispic) </script>
    //right here I put the image. This part works fine. I get the next
    image, then the next...

    Whenever document.write is called, I get the original value of the
    variable. I have even put the changed value in the Window.status bar,
    and still document.write reads the original value.

    How do I pass the changed value to the document.write method?

    Thank you -- again, I apologize for what is surely a novice question.

    Judy M
  • Lasse Reichstein Nielsen

    #2
    Re: passing variables

    Judy_Myers@hotm ail.com (Judy M) writes:
    [color=blue]
    > I am a Javascript Newbie, but have read two of the brick-sized books
    > and read the deja section and FAQ's and didn't find my answer. I'm
    > sure it is simple and apologize in advance, but here goes...[/color]
    ....[color=blue]
    > var thispic=0[/color]
    ....[color=blue]
    > function.proces snext() {
    > if document.images && thispic < imgct {[/color]

    Need parentheses around the condition.

    Generally, there are a lot of typos. That is why it is better to
    create a small example file that shows the problem (with lines no
    longer than 72 characters) and then include that file verbatim.
    Then we won't have to sift through the typos in order to find the
    serious error.
    [color=blue]
    > thispic++
    > document.mypict ure.src=mypix[thispic][/color]

    It is safer to access the image through the images collection:

    document.images['mypicture'].src=mypix[thispic];
    [color=blue]
    > a href="javascrip t.processnext() "> Next &gt; &gt;</a>[/color]

    There are plenty of good reasons against using the javascript: pseudo
    protocol. You can use the onclick event instead.
    <URL:http://jibbering.com/faq/#FAQ4_24>
    [color=blue]
    > <script.languag e="javascript " type="text.java script"
    > document.write( thispic) </script>
    > //right here I put the image. This part works fine. I get the next
    > image, then the next...[/color]
    [color=blue]
    >
    > Whenever document.write is called, I get the original value of the
    > variable. I have even put the changed value in the Window.status bar,
    > and still document.write reads the original value.[/color]

    The important point here is *when* the code is executed. Since the web
    page is event driven, you cannot assume that execution starts at the
    top of the file and continues downwards.

    The contents of your script tags is executed in order, and this
    happens while the page is being loaded. That is why "document.write "
    can put content into the page: the page is still being loaded at that
    point, and "document.write " merely inserts data into the stream
    being parsed.

    The code that increments "thispic" is in the function "processnex t".
    That code is not executed until someone clicks on one of the links. At
    that time, the page has been loaded for a while, and the
    "document.write "'s have already been executed, using the value of
    "thispic" current at that time. It is not executed again when the
    value of "thispic" changes". That is why you don't see the new value.
    [color=blue]
    > How do I pass the changed value to the document.write method?[/color]

    You don't. If you want to change the contents of a part of the
    page, you don't use "document.write ". That function is restricted
    to injecting data into a page that is currently being opened.

    To change the content of the page, try:

    Add a container for the data:
    <span id="spanId">0</span>
    (instead of "document.write (thispic)" at a time when we know "thispic"
    has the value 0)
    Then, to change the content, add this to processnext after "thispic++" :
    document.getEle mentById("spanI d").firstChild. nodeValue = thispic;
    [color=blue]
    > Thank you -- again, I apologize for what is surely a novice question.[/color]

    It is, but no need to apologize for that.

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • George Ziniewicz

      #3
      Re: passing variables

      "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
      news:4r0mmjb7.f sf@hotpop.com.. .[color=blue]
      > Judy_Myers@hotm ail.com (Judy M) writes:
      >[color=green]
      > > I am a Javascript Newbie, but have read two of the brick-sized books
      > > and read the deja section and FAQ's and didn't find my answer. I'm
      > > sure it is simple and apologize in advance, but here goes...[/color]
      > ...[color=green]
      > > var thispic=0[/color]
      > ...[color=green]
      > > function.proces snext() {
      > > if document.images && thispic < imgct {[/color]
      >
      > Need parentheses around the condition.
      >
      > Generally, there are a lot of typos. That is why it is better to
      > create a small example file that shows the problem (with lines no
      > longer than 72 characters) and then include that file verbatim.
      > Then we won't have to sift through the typos in order to find the
      > serious error.
      >[color=green]
      > > thispic++
      > > document.mypict ure.src=mypix[thispic][/color]
      >
      > It is safer to access the image through the images collection:
      >
      > document.images['mypicture'].src=mypix[thispic];[/color]

      Thanks for that tip, how is it safer? (what browser/version/future aspect
      is affected?)

      I keep seeing these associative arrays being used vs. the equivalent
      functions (or so I thought). Is the use of document.all[]and the various
      images and forms arrays better to use for cross-browser support?



      [color=blue][color=green]
      > > a href="javascrip t.processnext() "> Next &gt; &gt;</a>[/color]
      >
      > There are plenty of good reasons against using the javascript: pseudo
      > protocol. You can use the onclick event instead.
      > <URL:http://jibbering.com/faq/#FAQ4_24>
      >[color=green]
      > > <script type="text.java script"[/color][/color]

      Safe to drop the <language="java script"> part?


      [color=blue][color=green]
      > > document.write( thispic) </script>
      > > //right here I put the image. This part works fine. I get the next
      > > image, then the next...[/color]
      >[color=green]
      > >
      > > Whenever document.write is called, I get the original value of the
      > > variable. I have even put the changed value in the Window.status bar,
      > > and still document.write reads the original value.[/color]
      >
      > The important point here is *when* the code is executed. Since the web
      > page is event driven, you cannot assume that execution starts at the
      > top of the file and continues downwards.[/color]

      That took me a while to grasp, that the body is generally executed only
      once (to sort of set things up), after that only the javascript functions
      ever "run". There is an internal copy/translation of the body for certain
      purposes, but it basically only runs once. Anything wanted refreshed or
      recalculated on a regular basis must run through a function or event of some
      kind.



      [color=blue]
      > The contents of your script tags is executed in order, and this
      > happens while the page is being loaded. That is why "document.write "
      > can put content into the page: the page is still being loaded at that
      > point, and "document.write " merely inserts data into the stream
      > being parsed.
      >
      > The code that increments "thispic" is in the function "processnex t".
      > That code is not executed until someone clicks on one of the links. At
      > that time, the page has been loaded for a while, and the
      > "document.write "'s have already been executed, using the value of
      > "thispic" current at that time. It is not executed again when the
      > value of "thispic" changes". That is why you don't see the new value.
      >[color=green]
      > > How do I pass the changed value to the document.write method?[/color]
      >
      > You don't. If you want to change the contents of a part of the
      > page, you don't use "document.write ". That function is restricted
      > to injecting data into a page that is currently being opened.
      > To change the content of the page, try:
      >
      > Add a container for the data:
      > <span id="spanId">0</span>
      > (instead of "document.write (thispic)" at a time when we know "thispic"
      > has the value 0)
      > Then, to change the content, add this to processnext after "thispic++" :
      > document.getEle mentById("spanI d").firstChild. nodeValue = thispic;
      >[color=green]
      > > Thank you -- again, I apologize for what is surely a novice question.[/color]
      >
      > It is, but no need to apologize for that.
      >
      > /L
      > --
      > Lasse Reichstein Nielsen - lrn@hotpop.com
      > Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
      > 'Faith without judgement merely degrades the spirit divine.'[/color]

      zin
      --
      Photography and Mysticism - http://www.zintel.com


      Comment

      Working...