Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Mar 2003
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Recursive function called within the "document.write" command doesn't work.

    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
    show_img(i);
    }
    else if (i==galeria.length-1){...}
    }

    //function "show_img" is called by "display_image"
    function display_image(i){
    w=window.open("slide_show.htm");
    show_img(i);
    }

    I appreciate your help.
    Thank you

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    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>')

  • #3
    New to the CF scene
    Join Date
    Mar 2003
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Glenn,
    Thank you very much! I should have thought about that since show_img() worked outside w.document.write...

  • #4
    New to the CF scene
    Join Date
    Mar 2003
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy

    Unfortunately, it still does not work... I still get the same error...

  • #5
    Regular Coder
    Join Date
    Nov 2002
    Posts
    596
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •