Link to home
Start Free TrialLog in
Avatar of freespirit020199
freespirit020199Flag for United States of America

asked on

Auto Close a popup window containing an image only (no html).

I have a new smaller window pop up when someone clicks on a thumbnail image on my page. I do this by using the following code:

<a target="_self" onclick="window.open('directory/largerimage1.jpg','window1','scrollbars=yes,width=200,height=200')"
        href="samepage.htm"><img src="directory/thumbnailimage1.jpg" WIDTH="60" HEIGHT="60">

I would like to know how to get this window to self close after a specified time so that they do not end up having a number of windows open after clicking on the various thumnails present.

I found answers saying to place Java Script in the new window telling it to self close after X seconds. However since my new window points only to an image file (jpg or gif) there is no html in the new window containing the <head> tag nor a <body> tag, just a directory/image.jpg. Please help me I am not html savvy but trying my best to understand what I need to to get this page done soon. Help.
Avatar of knightEknight
knightEknight
Flag of United States of America image

You mean something like this?:

<SCRIPT language='javascript'>
  var winh = window.open("","window1");  // this gets a handle to window1
  setTimeout( "if ( winh && !winh.closed ){winh.close();}", 30000 );  // close the window after 30 seconds

</script>
Try this:

<HTML>
<HEAD>
<SCRIPT language='javascript'>

 function closewin( windowname )
 {
      var winh = window.open( "", windowname );  // get handle to window
      if ( winh && !winh.closed )
      {
          winh.close();
      }
}

 function openwin( theURL )
 {
    var windowname =  (""+Math.random()).substring(1);
    var winh = window.open( theURL, windowname, 'scrollbars=yes,width=200,height=200');
    setTimeout( 'closewin("'+windowname+'");', 30000 );
 }

</script>

</head>
<BODY>



<a target="_self" onClick="openwin('directory/largerimage1.jpg');return(false);"   href="samepage.htm"><img src="directory/thumbnailimage1.jpg" WIDTH="60" HEIGHT="60" border="0"></a>

</body>
</html>
... adjust the milliseconds in the setTimeout to your liking.
one typo -- in the openwin() function change:  substring(1);   to:  substring(2);
Avatar of kevcole2
kevcole2

I think there's an easier way...

This should work, put it in your body statement, ie:

<body onLoad="setTimeout(window.close, 10000)">

The number there is milliseconds I believe. Should work...did for me at least...

Kevin
Yes, but that will not work with the current code because the window is not opening an HTML page, but a .jpg file, so there is no BODY tag (or any other tag for that matter).
any thoughts, freespirit?
Avatar of freespirit020199

ASKER

Adjusted points from 50 to 60
Thanks everyone...I apologize for the delay but I was unable to come back to this project until now.

knightEknight, your script worked beautifully. You were correct about the fact that the pop-up window had no body codes as it was a simple image file, which is why the other answer would not work. I intend to give you the points. One last clarification however: the size of the new pop-up windows are determined in the script placed in the <head> tag. Firstly will this work with multiple thumbnails on a single page, for multiple pop-up wndows per page? Also the new window often needs to be different sizes depending on the larger image. In my previous code, I was able to determine that by:

onclick="window.open('directory/largerimage1.jpg','window1','scrollbars=yes,width=200,height=200')"

for each window.open command. Your way is great except for the fact that the new window size is fixed for every thumbnail. How can I get around that?
ASKER CERTIFIED SOLUTION
Avatar of knightEknight
knightEknight
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry, my typing is not the best today (it's Monday)

"elagorate"  should be  "elaborate",  as one example.
As you requested, the page with my problem code is at:
http://www.newworldrealtors.com/showcase/showcaseindex.htm

I have just replaced the previous code with the one above and it seems to be working fine. I have not uploaded it as yet though so you would see your code in action probably within a few days.

I realize that the code you so kindly gave me opens each window in a separate window. This is fine since they each close after the seconds set. But for future's sake, is there a way to fine tune it so that each time someone clicks a separate thumbnail it opens into the SAME window? Is this asking too much? If so, then no prob... as it works fine as is.

Thanks again. Will be back to check or accept the answers as soon as I get this uploaded.
knightEknight:
P.S. Is there a way to contact you regarding your services and fees, if so required in the future?
How about if we close the popup as soon as another one is opened?  Try this:

function closewin( windowname )   // the only change here is to rename winh to winh2
{
  var winh2 = window.open( "", windowname );  // get handle to window
  if ( winh2 && !winh2.closed )
  {
     winh2.close();
  }
}

var winh=null;  // move this declaration outside the function to make it global.

function openwin( theURL, w, h )
{
   if ( winh && !winh.closed )   // if a previous window is still open, then close it immediately
      winh.close();

   var windowname =  (""+Math.random()).substring(2);
   winh = window.open( theURL, windowname, 'scrollbars=yes,width='+w+',height='+h);
   setTimeout( 'closewin("'+windowname+'");', 30000 );
}

If you want my email address, post another question for 0 points.  I'll lock it, you accept it, and then I'll add my email address as a comment.  Thanks!
Thanks a million knightEknight
I used your solution at:
http://www.newworldrealtors.com/showcase/showcaseindex.htm

It does what you say and works fine. There is something that happens after the 30 seconds is up where briefly a blank browser page flashes before disappearing but I am not sure what that is about. But it does the job fine for me. This is much better than what i had tried to do before.

Thank you. I will post that question re the email so look out for it.