PROBLEM: window.open & accessing parent variables' values

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jiri Brada

    PROBLEM: window.open & accessing parent variables' values

    Hi,

    I have got a following problem with using Javascript:

    I have a HTML page with pictures' thumbnails. After clicking on any
    thumbnail, I would like to open a new window with the original size
    picture. In the main window with thumbnails, I have got following
    important stuff:

    <SCRIPT LANGUAGE="JavaS cript">

    var images = new Array (
    "2004_Croatia_B rela/Croatia_Brela_0 83.jpg",
    "2004_Croatia_B rela/Croatia_Brela_0 90.jpg",
    "2004_Croatia_B rela/Croatia_Brela_1 07.jpg",
    "2004_Croatia_B rela/Croatia_Brela_1 08.jpg"
    );
    var image_count = images.length-1;
    var pImageNumber = 0;

    function show_image( pImageNumber ) {

    var pFile = new Image();
    var ht = 0;
    var wd = 0;

    for ( var i=0; i<image_count; i++ ) {
    pFile.src = images[i];
    if ( pFile.height > ht ) ht = pFile.height;
    if ( pFile.width > wd ) wd = pFile.width;
    }

    pFile.src = images[pImageNumber];
    ht = ht + 20 + 40;
    wd = wd + 20;

    imagewindow = window.open( "GSET_Croatia_B rela_images.htm ", "img",
    "innerWidth =" + wd + ",width=" + wd + ",innerHeig ht=" + ht +
    ",height=" + ht + ",resizable,lef t=20,top=20");

    // this is commented because I always get a script
    // error that "imagewindow.do cument.images.c hanging_image" is
    // null or does not exist - is it because the new page is not loaded
    yet?

    //imagewindow.doc ument.images.ch anging_image.sr c = pFile.src;
    //imagewindow.doc ument.images.ch anging_image.wi dth = pFile.width;
    //imagewindow.doc ument.images.ch anging_image.he ight = pFile.height;
    //imagewindow.doc ument.image_num ber = pImageNumber;
    }
    </SCRIPT>

    and somewhere in the body I have got

    <a href="2004_Croa tia_Brela/Croatia_Brela_1 07.jpg"
    onClick="show_i mage(2); return false;"><img
    src="2004_Croat ia_Brela/Thumbnail_Croat ia_Brela_107.jp g" width="154"
    height="115" border="0"></a>

    Now, the new page with original images (GSET_Croatia_B rela_images.htm )
    looks like:

    <html>
    <head>
    <title>GS Radov&aacute;nk y</title>
    <meta http-equiv="Content-Type" content="text/html;
    charset=iso-8859-1">
    <link href="Assets/GSET_CSS_Style. css" rel="stylesheet "
    type="text/css">
    <SCRIPT LANGUAGE="JavaS cript">

    var images = new Array (
    "2004_Croatia_B rela/Croatia_Brela_0 83.jpg",
    "2004_Croatia_B rela/Croatia_Brela_0 90.jpg",
    "2004_Croatia_B rela/Croatia_Brela_1 07.jpg",
    "2004_Croatia_B rela/Croatia_Brela_1 08.jpg"
    );
    var image_number = 0;
    var image_count = images.length-1;
    var current_image = new Image();

    function PreviousImage ( ) {
    image_number--;
    if ( image_number == -1 ) image_number = image_count;
    current_image.s rc = images[image_number];
    document.images .changing_image .src = current_image.s rc;
    document.images .changing_image .width = current_image.w idth;
    document.images .changing_image .height = current_image.h eight;
    }
    function NextImage ( ) {
    image_number++;
    if ( image_number == image_count+1 ) image_number = 0;
    current_image.s rc = images[image_number];
    document.images .changing_image .src = current_image.s rc;
    document.images .changing_image .width = current_image.w idth;
    document.images .changing_image .height = current_image.h eight;
    }
    function ActualImage ( ) {
    image_number = opener.pImageNu mber;
    current_image.s rc = images[image_number];
    document.images .changing_image .src = current_image.s rc;
    document.images .changing_image .width = current_image.w idth;
    document.images .changing_image .height = current_image.h eight;
    }
    </SCRIPT>
    </head>
    <body onLoad="ActualI mage(); return false;">
    <table align="center" cellspacing="0" >
    <tr>
    <td align="center" valign="middle" ><img name="changing_ image"
    src="Images/Empty_image.jpg " onClick="NextIm age();" width="640"
    height="480" alt=""></td>
    </tr>
    <tr>
    <td align="center" valign="middle" ><table align="center"
    cellspacing="10 ">
    <tr align="center" valign="middle" >
    <td class="white_li nk"><a href="#"
    onClick="Previo usImage();">Pre vious</a></td>
    <td class="white_li nk"><a href="#"
    onClick="NextIm age();">Next</a></td>
    </tr>
    </table></td>
    </tr>
    </table>
    </body>
    </html>

    So, I though that by using the onLoad function for body I could set
    the correct image from the list (which is the same as the list on the
    thumbnail page, maybe uselessly?) by using image_number =
    opener.pImageNu mber in the ActualImage function executed within
    onLoad, but is simply does not work.

    Do you have any idea what I am doing wrong? I thought that by using
    opener keyword and image_number = opener.pImageNu mber I would be able
    to find out what value was used for running the show_image (
    pImageNumber) on the main page.

    Thank you for any help in advance.

    Jiri
  • Michael Winter

    #2
    Re: PROBLEM: window.open &amp; accessing parent variables' values

    On 4 Apr 2004 21:56:57 -0700, Jiri Brada <bradaj@gsl.c z> wrote:
    [color=blue]
    > <SCRIPT LANGUAGE="JavaS cript">[/color]

    The language attribute is deprecated. You should use the required type
    attribute:

    <script type="text/javascript">
    [color=blue]
    > var images = new Array (
    > "2004_Croatia_B rela/Croatia_Brela_0 83.jpg",
    > "2004_Croatia_B rela/Croatia_Brela_0 90.jpg",
    > "2004_Croatia_B rela/Croatia_Brela_1 07.jpg",
    > "2004_Croatia_B rela/Croatia_Brela_1 08.jpg"
    > );[/color]

    It's probably better to use the array literal notation:

    var images = [ '2004_Croatia_B rela/Croatia_Brela_0 83.jpg',
    '2004_Croatia_B rela/Croatia_Brela_0 90.jpg',
    '2004_Croatia_B rela/Croatia_Brela_1 07.jpg',
    '2004_Croatia_B rela/Croatia_Brela_1 08.jpg' ];

    [snipped beginning of show_image()]
    [color=blue]
    > imagewindow = window.open( "GSET_Croatia_B rela_images.htm ", "img",
    > "innerWidth =" + wd + ",width=" + wd + ",innerHeig ht=" + ht +
    > ",height=" + ht + ",resizable,lef t=20,top=20");
    >
    > // this is commented because I always get a script
    > // error that "imagewindow.do cument.images.c hanging_image" is
    > // null or does not exist - is it because the new page is not loaded
    > // yet?[/color]

    Yes, precisely.

    [snip]
    [color=blue]
    > <SCRIPT LANGUAGE="JavaS cript">
    >
    > var images = new Array (
    > "2004_Croatia_B rela/Croatia_Brela_0 83.jpg",
    > "2004_Croatia_B rela/Croatia_Brela_0 90.jpg",
    > "2004_Croatia_B rela/Croatia_Brela_1 07.jpg",
    > "2004_Croatia_B rela/Croatia_Brela_1 08.jpg"
    > );[/color]

    As above.

    [snip]
    [color=blue]
    > <body onLoad="ActualI mage(); return false;">[/color]

    Why are you returning false? It's not as if you can cancel loading.

    [snip]
    [color=blue]
    > <tr align="center" valign="middle" >
    > <td class="white_li nk"><a href="#"
    > onClick="Previo usImage();">Pre vious</a></td>
    > <td class="white_li nk"><a href="#"
    > onClick="NextIm age();">Next</a></td>[/color]

    You should cancel the click events after calling the functions.

    [snipped last bit of HTML]
    [color=blue]
    > So, I though that by using the onLoad function for body I could set
    > the correct image from the list (which is the same as the list on the
    > thumbnail page, maybe uselessly?) by using image_number =
    > opener.pImageNu mber in the ActualImage function executed within
    > onLoad, but is simply does not work.
    >
    > Do you have any idea what I am doing wrong? I thought that by using
    > opener keyword and image_number = opener.pImageNu mber I would be able
    > to find out what value was used for running the show_image (
    > pImageNumber) on the main page.[/color]

    What you're attempting should work fine, but check the show_image()
    function. At no point do you actually change the pImageNumber variable.

    Hope that helps,
    Mike

    --
    Michael Winter
    M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

    Comment

    Working...