passing variables

J

Judy M

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.processnext() {
if document.images && thispic < imgct {
thispic++
document.mypicture.src=mypix[thispic]
}
</head>
<body>
a href="javascript.processnext()"> Next &gt; &gt;</a>
<script.language="javascript" type="text.javascript"
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
 
L

Lasse Reichstein Nielsen

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... ....
var thispic=0 ....
function.processnext() {
if document.images && thispic < imgct {

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.
thispic++
document.mypicture.src=mypix[thispic]

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

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

There are plenty of good reasons against using the javascript: pseudo
protocol. You can use the onclick event instead.
<script.language="javascript" type="text.javascript"
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.

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 "processnext".
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.
How do I pass the changed value to the document.write method?

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.getElementById("spanId").firstChild.nodeValue = thispic;
Thank you -- again, I apologize for what is surely a novice question.

It is, but no need to apologize for that.

/L
 
G

George Ziniewicz

Lasse Reichstein Nielsen said:
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... ...
var thispic=0 ...
function.processnext() {
if document.images && thispic < imgct {

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.
thispic++
document.mypicture.src=mypix[thispic]

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

document.images['mypicture'].src=mypix[thispic];

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?



There are plenty of good reasons against using the javascript: pseudo
protocol. You can use the onclick event instead.

Safe to drop the <language="javascript"> part?


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.

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.



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 "processnext".
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.


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.getElementById("spanId").firstChild.nodeValue = thispic;


It is, but no need to apologize for that.

/L

zin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,079
Messages
2,570,574
Members
47,206
Latest member
Zenden

Latest Threads

Top