1

I'm new to JavaScript and jQuery, I know HTML & CSS rather well.

I have a page of 20 minimised pictures (soon to be 500+ images) that when clicked are opened into a new page.

I am having no issues there. What I would like is when a picture is clicked it is opened in an already styled page and the selected picture is placed in the center of that page (or where ever I would like it placed).

I know I must need to carry the picture id to the new page, but I am completely lost as to what code to use. Here is what I am using right now to get to the new page.

<img src="img/1.jpg" class="thumbnail" id="preview">

and to get this image to open in a new window I am using

<script>
$(document).ready(function() {
    $('img.thumbnail').click(function() {
        window.location.href = this.id + '.html';
    });
});
</script>

This code opens the image in another window but it is not styled, I would like to have it styled and have any image that is selected placed where I select it to be.

I don't want to create 500+ pages for each image and I know I have seen it done, I just don't know how to do it.

Thank you for any assistance that can be offered.

Mark
  • 6,762
  • 1
  • 33
  • 50
Wardever
  • 13
  • 2

3 Answers3

1

Why not pass the value of src attribute to page as a querystring with jQuery?

window.location.href = 'preview.html?src='+$(this).attr('src');

Then in preview.html, fetch the src parameter with javascript using the function provided here.

Example:

var src = getParameterByName('src');

It would be a lot more simple if you're able to use server-side scripting though.

Community
  • 1
  • 1
rasso
  • 2,131
  • 3
  • 21
  • 24
1

I know I must need to carry the picture id to the new page

Probably not, if you want to display a styled image I'd expect you to want to either request the image via an Ajax request, or perhaps by using a known naming convention if you wanted to use your id based approach.

The reason I say, not a new page, is that it's going to be easier to maintain your site styling if you say, load a modal dialog or something along those lines to display your photos.

There are many modal dialog plugins which will handle images such as the zurb reveal plugin:

http://foundation.zurb.com/docs/components/reveal.html

There are also a whole range of photo specific jquery plugins which will give you added funtionality like previous / next, descriptions, and keyboard navigation.

Whilst you could use the approach you describe and open them in a separate page, I would suggest investigating using a plugin to avoid needing to re-invent the wheel.

I did a quick google, and this one looked interesting: http://galleria.io/ but there are hundred of similar tools out there.

dougajmcdonald
  • 19,231
  • 12
  • 56
  • 89
-1

use the image as GET param like this:

$('img.thumbnail').click(function() {
    window.location.href = 'template.html?image='+ encodeURIComponent( $(this).attr("src"));
});

then inside your template.html prepare a layout for your image and

<img src="<? echo urldecode($_GET['image']); ?>" />
Vanojx1
  • 5,574
  • 2
  • 23
  • 37