ZenphotoCMS Forum
Mozilla link prefetching using <link> tag - Printable Version

+- ZenphotoCMS Forum (https://forum.zenphoto.org)
+-- Forum: Support (https://forum.zenphoto.org/forum-1.html)
+--- Forum: General support (https://forum.zenphoto.org/forum-4.html)
+--- Thread: Mozilla link prefetching using <link> tag (/thread-1823.html)



Mozilla link prefetching using <link> tag - teedog - 2007-11-19

I would very much like the browser to prefetch or preload the next image in the particular album to optimize browsing performance. I realize that Test-JS has a JavaScript function for that purpose, but that theme is for testing purposes only at the moment. Would the `` tag be a simple way of making Mozilla-based browsers prefetch the next image? Read about link prefetching here.

I inserted this code in the header section of image.php:

`




Mozilla link prefetching using <link> tag - teedog - 2007-11-19

I was able to get the link of the next default sized image using the following code:

`if (hasNextImage()) {

next_image();

echo '';

}`

However, how do I return the context to the image I was on before I moved forward with next_image()?

I tried set_context(ZP_IMAGE); but that sends me to the first image in the album even if I wasn't viewing that image.




Mozilla link prefetching using <link> tag - sbillard - 2007-11-19

You will have to save &#36;_zp_current_image somewhere then restore it.

However, the approach you are taking will have problems with image pagination. I think a better approach is to use something like:

`&#36;imagarray = &#36;_zp_current_album->getImages(0);

&#36;nextimage = &#36;imagearray[imageNumber()+1];`

You do, of course, need to be sure there is a next image.




Mozilla link prefetching using <link> tag - teedog - 2007-11-19

I was able to get the name of the next image using

` &#36;imagearray = &#36;_zp_current_album->getImages(0);

&#36;nextimage = &#36;imagearray[imageNumber()];`

&#36;nextimage is now a text string, the file name of the next image. But how do I get the URL to the default sized image of that name? getDefaultSizedImage() doesn't take parameters and only returns the default sized image URL of &#36;_zp_current_image.

Thanks for your help.




Mozilla link prefetching using <link> tag - sbillard - 2007-11-19

YOu can make a new image object from the name:

&#36;nextimageobject = new Image(&#36;_zp_current_album, &#36;nextimage);

Then use &#36;nextimageobject->getCustomImage(....);

or &#36;nextimageobject->getThumb();




Mozilla link prefetching using <link> tag - teedog - 2007-11-19

Thank you thank you! The following code in image.php's head section seems to be working for me. This should make all Mozilla-based browsers prefetch the next page and image while you are viewing any image in an album, provided there is a next page/image and you are displaying the default sized image.

` `

Please let me know if there's anything wrong with it.




Mozilla link prefetching using <link> tag - aitf311 - 2007-11-19

Works like a charm for me! You just forgot the slash before the n on the links. Any chance of making this into a function? And do you know if mozilla-based browsers load this last?




Mozilla link prefetching using <link> tag - sbillard - 2007-11-19

This might be better from an efficiency standpoint:
` &#36;my_image_array = &#36;_zp_current_album->getImages(0);

&#36;n = imageNumber();

if (&#36;n < count(&#36;my_image_array) {

&#36;my_nextimage = &#36;my_image_array[&#36;n];

&#36;my_nextimage_object = new Image(&#36;_zp_current_album, &#36;my_nextimage);

&#36;my_nextimage_url = &#36;my_nextimage_object->getSizedImage(getOption('image_size'));

echo ''."n";

echo ''."n";

}`

Not sure why you have the two echo statements. Isn't the second enough?




Mozilla link prefetching using <link> tag - teedog - 2007-11-19

aitf311: Glad I'm not the only one interested in this, though I probably lack the experience to make this into a neat function.
"And do you know if mozilla-based browsers load this last?"
Yep. Mozilla-based browsers should only start prefetching links when the browser becomes idle, meaning every element on the current page and even on other open tabs are finished loading. See "How is browser idle time determined?" for technical details.

sbillard: Ah, so if (&#36;n < count(&#36;my_image_array)) is more efficient than if (hasNextImage())? Thanks for the tip.

If you mean why I have 2 prefetch hints, one for getNextImageURL() and one for &#36;my_nextimage_url, it's because the first is the URL to the next [b]page[/b], while the second is the URL to the next image itself. I suppose I should choose less confusing variable names.

If you mean why I don't combine everything into 1 echo statement, no reason. Just clearer to write.

zenphoto devs rock.

P.S. Hmm bbpress forums are stripping the slash in \n.




Mozilla link prefetching using <link> tag - tummblr - 2007-11-29

I made this feature into a plugin and modified the code a bit. From my rudimentary profiling, the above code took about 0.1 seconds to execute. This version takes about 0.01 seconds.

http://www.tummblr.com/my-code/zenphoto-plugin-link-prefetching-hints/