View Full Version : Recursive function called within the "document.write" command doesn't work.

03-04-2003, 06:14 AM
I am trying to use JavaScript to open a picture (from a picture gallery) in a new window. In addition, I want to install three links in this new window: first one to close the window, second to see the next picture and third to see the previous picture.
To do so, I built a table. In the first row is the "close window" link. In the second is the picture and in the third one are links to previous and next pictures.
Function "show_img" displays the table and its contents, but when I call it (within itself) to see the next (or the previous) picture I get an "Error on page" error.

Here is a piece of my code:
var gallery=new Array ("view1.GIF","view2.GIF", "view3.GIF", "view4.GIF")

var i=0;
var powrot=new String("Go back to the gallery");
var pop=new String("Previous");
var nas=new String("Next");

function show_img(i){
w.document.write('<table align="center">')
w.document.write('<tr valign="top" align="right"><td colspan="2">')
w.document.write('<a href="javascript:close()">'+powrot+'</a></td></tr>')
w.document.write('<tr><td align="center">')
w.document.write('<img src="'+gallery[i]+'"></td></tr>')
if (i==0){
w.document.write('<tr><td valign="bottom" align="right">')
i = i + 1;
//here "show_img" doesn't work
w.document.write('<a href="javascript:show_img(i)">'+nas+'</td></tr></table>')
//here it works
else if (i==galeria.length-1){...}

//function "show_img" is called by "display_image"
function display_image(i){

I appreciate your help.
Thank you

03-04-2003, 06:36 AM
it is because show_img() function is in the main window (opener), not in the popup window. you should call opener.show_img(i) like this:

w.document.write('<a href="javascript&#58;opener.show_img(i)">'+nas+'</td></tr></table>')

you may want to check first if the opener window is still open or not since the user can close it and leave the popup open.

w.document.write('<a href="javascript&#58;if (opener && !opener.closed) opener.show_img(i)">'+nas+'</td></tr></table>')

03-04-2003, 05:48 PM
Thank you very much! I should have thought about that since show_img() worked outside w.document.write...

03-04-2003, 06:26 PM
Unfortunately, it still does not work... I still get the same error...

03-04-2003, 11:27 PM
w.document.write('<a href="#" onclick="if(opener&&!opener.closed)return opener.show_img(opener.i)">'+nas+'</td></tr></table>')

function show_img(i){
else if (i==galeria.length-1){...} //what is this?
return false;

This whole approach is kludgy, but referencing everything from the opener window should help.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum