Click to See Complete Forum and Search --> : Passing variables from a function to another


pfrize
05-20-2003, 04:36 PM
I am trying to code this structure :
- "collection" (level zero) shows a list of photo albums with links
- "album" (level one) shows photos thumbnails with links
- "photo" (level two) shows a full-size photo
I cannot manage :
- to pass the variables onto level two
- to link from level two back to level one
Below is a simplified template code with a structure similar to the one I am trying to implement. I would be very grateful to anyone who could tell me what I am doing wrong, and how to make it work.

Philippe
=============
test.htm
---------------------
<HTML><HEAD>
<script>
hw = 'Hello World';
function levelone() {
var hw = this.hw;
with (document) {
open();
writeln('<HTML><HEAD><TITLE></TITLE>');
writeln('<SCR' + 'IPT type="text/javascript" src="leveltwo.js"></scr' + 'ipt>');
writeln('</HEAD><BODY>');
writeln('LEVEL ONE<br><br>');
writeln(hw);
writeln('<br><br>');
writeln('<a href="javascript:leveltwo()">To Level Two</a><br><br>');
writeln('<a href="javascript:window.location.href=window.location.href;">Back</a>');
writeln('</BODY></HTML>');
close();
}
}
</script>
</HEAD><BODY>
<script>
document.write('LEVEL ZERO<br><br>');
document.write(hw + '<br><br>');
document.write('<a href="javascript:levelone()">To Level One</a>');
</script>
</BODY></HTML>
=============
leveltwo.js
---------------------
function leveltwo() {
var hw = this.hw;
with (document) {
open();
writeln('<HTML><HEAD><TITLE></TITLE></HEAD><BODY>');
writeln('LEVEL TWO<br><br>');
writeln(hw);
writeln('<br><br>');
writeln('<a href="javascript:document.location.href=document.location.href;">Back</a>');
writeln('</BODY></HTML>');
close();
}
}
============

Jona
05-20-2003, 05:25 PM
I'm not exactly sure what you want, but I would take out the hw=this.hw; line from your functions. They should be readable regardless, since you declared the variable global.

pfrize
05-20-2003, 05:45 PM
Jona,

You are right, I added that statement just in case it would make things better... but it did not. Basically:
- a first page shows the hw variable ("Hello World") and a hyperlink to call the levelone() function.
- the levelone() function opens a new page showing the hw variable and a link to call the leveltwo() function.
- the leveltwo() function should open a new page showing the hw variable.
This last one does not work : hw is shows as "undefined".

In the real world, the links on my "collection" page (showing a series of photo albums) work fine, but the links on the "album" pages (showing a series of photo thumbnails) do not because all passed variables show as undefined. You can have a look at :
http://www.frize.de/collection/collection.htm

Philippe

Jona
05-20-2003, 05:59 PM
This is because you are communicating with multiple pages rather than frames or windows. You cannot access variables from an unloaded, or not currently in memory, Web page.

You should also be using a server-side language for this, and not JavaScript. For the users that visit your site and do not have JavaScript enabled, they will see a blank page and only a blank page. You should not do this. Also, your site does not meet the recommendations of the W3C: http://validator.w3.org/ reports that your document does not have a DTD (Document Type Declaration) and it also lacks a character set. This is just for your information to help you become a better Web Developer. ;)