how to close a popup when another is opened?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
garyed
Forum Newbie
Posts: 13
Joined: Fri Feb 20, 2009 6:45 pm

how to close a popup when another is opened?

Post by garyed »

I have a web page with a bunch of small jpg's that will enlarge in a popup window when clicked.
The problem I have is if one is opened & not closed & another of the small jpg's is clicked on then the popup
will not show on the screen. It will just drop to the bottom menu bar. I really only want one popup to show at a time so is there a way to
set the window.open or something similar so it will automatically close whatever else is opened or even just take its place?
Here is some of my code:

Code: Select all

 
 
// Function to open up pictures in a popup window, an example of how its used is below 
// <img style="..." alt="..." src="s1carib.jpg" onclick="pictures('01carib.jpg')">
 
function pictures(jimage)
{
window.open (jimage,"The_Pics","menubar=no,statusbar=no,location=no,toolbars=no,scrollbars=yes,width=675,height=675,resizable=0,top=50,left=50");
}
 
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: how to close a popup when another is opened?

Post by jackpf »

If you have a title for your window, which you do ("The_Pics") then the window should only open once, and any further requests with that title will be opened in the same window.

Is that not the case?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: how to close a popup when another is opened?

Post by jackpf »

Sick, that was my 666th post :twisted:
garyed
Forum Newbie
Posts: 13
Joined: Fri Feb 20, 2009 6:45 pm

Re: how to close a popup when another is opened?

Post by garyed »

jackpf wrote:If you have a title for your window, which you do ("The_Pics") then the window should only open once, and any further requests with that title will be opened in the same window.

Is that not the case?
Well yes the window only opens once but it gets minimized down the bottom of the screen if its not closed before opening another window.
I hope its O.K. to post url here but its a lot easier to see what's happening when you click a picture with one already opened.

http://www.brokerbythesea.com/island_pics.html
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: how to close a popup when another is opened?

Post by jackpf »

Oh right, I see.

Well, I don't think you can really avoid that with window.open().

You could, however, close the window when they go back to the main page, and then open it again when they click on the next image. Just put this in the child window:

Code: Select all

<script>
    window.onblur = function()
    {
        window.close();
    }
</script>
garyed
Forum Newbie
Posts: 13
Joined: Fri Feb 20, 2009 6:45 pm

Re: how to close a popup when another is opened?

Post by garyed »

Thanks,
I'll try that out & see how it works.
garyed
Forum Newbie
Posts: 13
Joined: Fri Feb 20, 2009 6:45 pm

Re: how to close a popup when another is opened?

Post by garyed »

jackpf wrote:Oh right, I see.

Well, I don't think you can really avoid that with window.open().

You could, however, close the window when they go back to the main page, and then open it again when they click on the next image. Just put this in the child window:

Code: Select all

<script>
    window.onblur = function()
    {
        window.close();
    }
</script>
I'm not having much success.
I'm not sure where to exactly put that code & how to execute it.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: how to close a popup when another is opened?

Post by jackpf »

Okaaaay..... 8O

Anyway, this is what I suggest:

Have a php page called img.php, or whatever.

Then, instead of linking to the image, link to img.php?img=the_image_to_display.jpg

And then in img.php:

Code: Select all

<?php
if(isset($_GET['img']))
{
$img = basename($_GET['img']);
echo '<script>
    window.onblur = function()
    {
        window.close();
    }
</script>
 
<img src="images_dir/'.$img.'" alt="'.$img.'" />';
}
else
{
echo 'No image defined';
}
?>
Something like that anyway.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: how to close a popup when another is opened?

Post by pickle »

You should be able to assign the new window to a variable, then focus() that variable:

Code: Select all

var fullWindow = window.open(...);
fullWindow.focus();
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: how to close a popup when another is opened?

Post by jackpf »

Oh wow, I didn't know you could do that.
Post Reply