Forum Moderators: open

Message Too Old, No Replies

How do I create a PopUp on an image click?

         

midi25

2:04 pm on Jan 11, 2005 (gmt 0)

10+ Year Member



Hi I want to be able to display a pop when a user clicks on a thumbnail. The popup should show the image and details about the image eg text.

I assume I need to use Window.Open somehow.

Can anyone help with some code examples too.

Thanks

rocknbil

7:26 pm on Jan 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



// establish the 4 variables to use throughout

function newWindow(img,title,w,h) {

// Creates a relatively UNIQUE window id so that subsequent
// links don't load in the same window
var day =new Date();
var id = day.getTime();
var wt = w+50; // make room for scrollbars
var ht = h+125; // scrollbars and close button

//Personally, I think developers who ignore the screen height and allow
// the window to go off the bottom of the screen should have their feet cut off,
// just like their windows on smaller monitors.
if ((screen.height) && (ht > screen.height-150)) {
ht = screen.height-150;
}

var params = 'width='+wd+',height='+ht'+',scrollbars';

// TWO OPTIONS HERE: open a new window and doc. write or load a URL
// OPTION 1

var win = open('',id,params);
win.document.write('<html><head><title>'+title+</head><body><div align="center">+<h3>'+title+'</h3>\n');
win.document.write('<img src="images/'+img+'" width="'+w+'" height="'+h+'" border="0" alt="'+title+'"><br>\n');
win.document.write('<form><input type="button" onClick="window.close();" value="Close Window"><br></body></html>');
//A common mistake for newbies: closing the doc flushes the buffer.
// Not closing it prints blank window
win.document.close();

// OPTION 2: this requires only passing w and h
// (window width and height) and loads an actual
// HTML file instead of an image for img.

// var win = open(img,id,params);

} // Dass all

To call it with method 1:

<a href="javascript:newWindow('large_img.jpg','My Image',300,250);"><img src="images/thumbnail.jpg" width="75" height="50" border="0" alt="Small Image"></a>

Method 2, linked to an HTML page, just sub html for image and leave title blank:

<a href="javascript:newWindow('large_image.html','',300,250);"><img src="images/thumbnail.jpg" width="75" height="50" border="0" alt="Small Image"></a>

The previous has not been debugged and may contain errors, but I use it all the time without trouble. So far. :-)