I'm building an application that is an image gallery but with lots of images (it's for browsing archives of documents); basically the application does this:
- display an image
- display "previous" and "next" links left and right of the first image
The previous and next links are retrieved from an XML file that lists all images in sequence; there are around 1000-2000 images in each set (one XML file per set). In order to avoid having to look for preceding and following sibling elements, I included the names of each previous and next images as attributes of each image.
So the XML file is basically:
<list>
...
<image previous="ww.jpg" next="yy.jpg">xx.jpg</image>
<image previous="xx.jpg" next="zz.jpg">yy.jpg</image>
<image previous="yy.jpg" next="aa.jpg">zz.jpg</image>
...
</list>
What I'm doing is this:
function display(img) {
var lookingfor = 'image:contains('+img+')';
$.ajax({
type: "GET",
url: "pns.xml",
dataType: "xml",
success: function(xml) {
$(xml).find(lookingfor).each(function(){
// display the actual image and the links from @previous and @next attributes
});
}
});
}
$(document).ready(function(){
var image = $.query.get('img');
display(image);
});
The first time the page loads, the script:
- gets the name of the image to be displayed from the url parameter 'img'
- loads the xml file containing the name of all images
- gets previous and next image names from this same file
- displays the image
- displays links to the previous and next images
So far so good. My problem is with the other images.
The link to the previous or next image is:
display('image_id');
I was hoping that without reloading the page and simply calling again the 'display' function I would not have to get the XML file again, and not have to parse it again. However, it seems to not be the case: the XML file is apparently loaded again with every new call of the 'display' function.
A significant delay the first time the page is loaded is acceptable; but the same delay for each image seems sluggish and very unpleasant.
I have full control over the application; if there is a more efficient solution than an XML file to hold the parameters that would be fine. However the application has to work offline, so I cannot query a server to get the names of preceding / following images.
Also, using the :contains() selector is probably not the fastest approach: what would be better?
Edit: using XML and loading it externally was clearly far from optimal; a much better and simpler approach is to have a big object containing one array per image and to load this object just once (when the script is first loaded). Another approach would be to use some local storage / SQL facility; I may try this with Google Gears which provide such tools.