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.
freespirit020199Asked:
Who is Participating?
 
knightEknightConnect With a Mentor Commented:
I modified the openwin function to accept to additional parameters for the width and height of the image.  That way you can specify the w/h in the onClick event as shown below.  It's hard for me to say exactly how to apply this to your situation without seeing the code you are using to determine the w/h of each image.  Please paste that code and I can elagorate further (I'd like to see that myself anyway!)


<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, w, h)
{
   var windowname =  (""+Math.random()).substring(2);
   var winh = window.open( theURL, windowname, 'scrollbars=yes,width='+w+',height='+h);
   setTimeout( 'closewin("'+windowname+'");', 30000 );
}

</script>
</head>

<BODY>


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

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

</body>
</html>
0
 
knightEknightCommented:
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>
0
 
knightEknightCommented:
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>
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
knightEknightCommented:
... adjust the milliseconds in the setTimeout to your liking.
0
 
knightEknightCommented:
one typo -- in the openwin() function change:  substring(1);   to:  substring(2);
0
 
kevcole2Commented:
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
0
 
knightEknightCommented:
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).
0
 
knightEknightCommented:
any thoughts, freespirit?
0
 
freespirit020199Author Commented:
Adjusted points from 50 to 60
0
 
freespirit020199Author Commented:
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?
0
 
knightEknightCommented:
Sorry, my typing is not the best today (it's Monday)

"elagorate"  should be  "elaborate",  as one example.
0
 
freespirit020199Author Commented:
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.
0
 
freespirit020199Author Commented:
knightEknight:
P.S. Is there a way to contact you regarding your services and fees, if so required in the future?
0
 
knightEknightCommented:
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!
0
 
freespirit020199Author Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.