views:

56

answers:

4

Hi,

How can i load the original image when the tumbnail version of the image has been clicked? Im using ASP.NET in combinaton with javascript.

The original images are big, so they have been scaled on server side. This makes the site load faster. But somehow, both versions (original and tumbnail) of the images are being downloaded.

I'm trying to download only the tumbnail version of the image. And when the user clicks on the image, i want to show the original image.

How can i get this done?

+3  A: 

Html such as below for each thumbnail image should do the trick

<a href="[url to original image]" target="_blank" id="thumbnail_link">
  <img src="[url to thumbnail image]" alt="Click to see the full image" />
</a>

Edit: Modified to illustrate use of FancyBox.

Use above markup along with below java-script:

$(document).ready(function() {
   $("a#thumbnail_link").fancybox();
})'

Don't forget to include jquery and fancybox js files.

VinayC
I'm not sure linking to the full size image is what he's after...
TimS
@TimS, above code will show full size image in a new window when clicked on thumbnail. Unless, he want to show the full image in a layer within page, I would prefer above mark-up as it does not depend on java-script.
VinayC
The question is entitled "show image onclick javascript" :D
TimS
@TimS - now updated to use JS :-)
VinayC
A: 

I think you have to show thumbnails first and on click you need to open the original images in a new pop up window. You can do this using code as given below -

<SCRIPT LANGUAGE="JavaScript">
function openImage(imageFile){
windowOpen=window.open("",'Open','toolbar=no,location=no,directories=no,menubar=no,scrollbars=no,resizable=1,width=420,height=420');
windowOpen.document.writeln("<head><title>Image</title></head><body>");
windowOpen.document.writeln('<img src=http://www.mysite.com/' + imageFile + 'border=1>');
windowOpen.document.writeln("</body></html>");
}
</SCRIPT>

Then call this openImage() method during onClick of the thumbnail image. You can pass imageFile as parameter to the function.

Sachin Shanbhag
Would work, but a popup window isn't necessary, as the image can be loaded into an overlay div.
TimS
Hi, i'm showing the images in the fancybox, which is javascript. www.fancybox.net.
Yustme
@Yustme - No idea about the fancybox dude, but the code in my answer is a javascript method. Not sure how you will use this method in fancybox tough.
Sachin Shanbhag
A: 

It sounds like you have both images referenced in your HTML, even though one is hidden from view, so the browser requests both. What you'd need to do is use JavaScript to create the full size <img> tag from scratch and then add it to the relevant place in the HTML. The browser will then load the full size image once it's added to the DOM.

TimS
But will it be added after a mouse click event? Because right now its downloading both. And this is unecessary.
Yustme
Exactly that, render the larger image's HTML on mouse click, don't just hide it in the HTML on page load and then use JS to show it later, as the browser will download it anyway.
TimS
Also, just a thought, have you tried using jQuery? It's perfect for what you're doing and you'll find a whole load of plugins which render the full size image in a nice box with a close / next / previous button. Just Google jQuery Image Gallery.
TimS
A: 

Hi Yustme,

For fancy box, all you need to do is

<a id="single_image" href="image_big.jpg"><img src="image_small.jpg" alt=""/></a>

Regards, Andy.

cherhan