Image popups that fit

šŸ‘‰ Whoa! Please note that this post is 18 years old. The views expressed within might have become outdated.

First of all; popup windows should not be used. Theyā€™re annoying, unfriendly thingies that should only be used as a last resort. Jakob Nielsen even states it the number 2 Web Design Mistake in his Top Ten. So donā€™t use ā€˜em! There are much better alternatives anyway. But if you really, really want to use popups, this article provides you with a handy approach.


Donā€™t rely on Javascript

Popup windows require Javascript. Javascript, being a client side scripting language and all, can be turned off by the user. Never rely on it.

Using bogus destinations in your links will get you nowhere:

<a href="#">click to open a window</a> With Javascript turned off, nothing will happen and your audience will be confused and thus annoyed. Besides, Google wonā€™t spider your precious content, since itā€™s not available in the href attribute. So instead, use something like this:

<a href="path/to/content/">click to see some content</a> This way, users with Javascript turned off will still be able to view your content. These are basic fundamentals when it comes to Javascript popups, A List Apart features more information if things are still unclear.

Popping up images

We now have the basics all sorted out, letā€™s focus on images. Regardless of popups being evil, theyā€™re still being used quite a lot by graphic artists, photographers et cetera in order to show their work in greater detail. As is especially the case with dynamic content, you might not know beforehand which dimensions to use when popping up images. The script below uses basic Javascript to calculate the imageā€™s dimensions before opening the new window. This will ascertain a perfect fit, as can be seen in this example.

All files can be downloaded as a RAR-archive.

Javascript calculations

Iā€™m going to be very brief in my explanation and just explain the general structure of the code. The script is pretty basic anyway and is written as a plug-and-play solution to be used by even the biggest development dummy.

function initPopups ()
{
	var W3CDOM = (document.getElementById && document.getElementsByTagName);
	if (!W3CDOM) return;
	var as = getElementsByClass ('popup',document,'a');
	for (var i = 0; i  0)
		{
			as[i].onclick = function ()
			{
				var imgsrc = this.href;
				var img = new Image();
				img.src = imgsrc;
				img.onload = function ()
				{
					var dims = [img.width,img.height];
					popImage(dims,imgsrc);
				}
				return false;
			}
		}
	}
}

This function is used to initialise the script. It uses Dustin Diazā€™s getElementsByClass method to fetch all a-elements with a ā€œpopupā€ class. It then assigns an onclick event handler to all of them which creates a new Image-object, with a src attribute containing the linkā€™s href value.

Take special care in this bit of code:

img.onload = function ()
{
	var dims = [img.width,img.height];
	popImage(dims,imgsrc);
}

You canā€™t fetch information about the image before it has completely loaded. Therefore, I assign an onload handler which triggers the next function:

function popImage (dims,imgsrc)
{
	var width = dims[0];
	var height = dims[1];
	var win = window.open ('image.html?url='+imgsrc,'popwin','width='+width+',height='+height);
}

This function then pops up a window as big as the image so it fits nicely inside. image.html fetches the URL of the image from the GET-variable ā€œurlā€ and creates an image element. In my example I added some extra functionality for closing the window by clicking on the image and displaying a message you can do so by mousing over the image.

window.onload = function ()
{
	var img = document.createElement ('img');

	var url = window.location.href.split('?');
	var imgsrc = url[1].substring(4);

	img.src = imgsrc;
	img.alt = '';

	var body = document.getElementsByTagName('body')[0];
	body.appendChild(img);

	var p = document.createElement ('p');
	p.id = 'close';
	p.appendChild(document.createTextNode ('Click to close window'));

	body.appendChild(p);

	img.style.cursor = 'pointer';
	img.onmouseover = function ()
	{
		p.style.display = 'block';
	}
	img.onmouseout = function ()
	{
		p.style.display = 'none';
	}
	img.onclick = function ()
	{
		window.close ();
	}
}

Some things to keep in mind

Note that in image.html I added some CSS rules to cancel out the default margin of the body element. I did this because the default margin would otherwise shove the image over the window borders, undoing everything weā€™ve achieved.

Also, the script is run within the onload event handler of the window object:

window.onload = initPopups; When using multiple scripts on your pages, that are all initialised in the onload handler, remember that only one method can be assigned to an event handler at a time. You can solve this problem with the following code:

window.onload = generalInit;
function generalInit ()
{
	initPopups();
	someOtherFunction();
}

To work, then! Upload the files to your server, include the following markup in your pagesā€¦

<a class="popup" href="someBigImage.jpg"><img alt="" src="someSmallImage.jpg"></img></a>ā€¦and include popup.js in the head of your documentā€¦

<script src="popup.js" type="text/javascript"></script>ā€¦but remember, kids: use ā€˜em sparingly!