Comments on: Preloading images from a directory https://j11y.io/snippets/preloading-images-from-a-directory/ Sun, 22 Mar 2015 15:39:22 +0000 hourly 1 https://wordpress.org/?v=5.0.13 By: Matt https://j11y.io/snippets/preloading-images-from-a-directory/#comment-405 Wed, 13 May 2009 00:04:50 +0000 https://j11y.io/?p=374#comment-405 Excellent script James, thanks for your work on it. I am fairly green with PHP and JavaScript, is there a way to pre-load not only all the images in a given directory but also in any sub-directories of the given directory?

Thank you

]]>
By: JD https://j11y.io/snippets/preloading-images-from-a-directory/#comment-404 Fri, 06 Mar 2009 23:15:59 +0000 https://j11y.io/?p=374#comment-404 looks like it blanked my codeblock….

was coded text

]]>
By: JD https://j11y.io/snippets/preloading-images-from-a-directory/#comment-403 Fri, 06 Mar 2009 23:15:02 +0000 https://j11y.io/?p=374#comment-403 What was the code block that you use in the foreach loop?
I tried using this “moments ago” and for some reason the
php code block destroys all coding below it? Weird…

insert method was just
[code] [/code]
JS on page. Any ideas?

]]>
By: Ibrahim https://j11y.io/snippets/preloading-images-from-a-directory/#comment-402 Wed, 07 Jan 2009 13:50:49 +0000 https://j11y.io/?p=374#comment-402 Thanks James 🙂

]]>
By: James https://j11y.io/snippets/preloading-images-from-a-directory/#comment-401 Wed, 07 Jan 2009 11:38:13 +0000 https://j11y.io/?p=374#comment-401 Hi Ibrahim, yep sure, I’ll explain:

It is a ternary operator which is really a shortcut to using if{}else{}. This is the format of a ternary operator:

(CONDITION) ? TRUE : FALSE;
 
// An example:
echo (2+2) == 4 ? 'YES' : 'NO'; // This will echo "YES"
]]>
By: Ibrahim https://j11y.io/snippets/preloading-images-from-a-directory/#comment-400 Wed, 07 Jan 2009 09:47:58 +0000 https://j11y.io/?p=374#comment-400 Hi James
Plz could you explain to me how this line works

 $arrayContents .= !empty($arrayContents) ? ',' : '';
 // i mean ? : ;
]]>
By: James https://j11y.io/snippets/preloading-images-from-a-directory/#comment-399 Wed, 07 Jan 2009 08:21:08 +0000 https://j11y.io/?p=374#comment-399 Thanks Vasili, I’ve changed it to a foreach loop; should be a little more efficient. 🙂

@Antione, I could have just used PHP to output the JSON within the page on the server-side instead of requesting it but I thought this might make it a little more interesting + you’re not limited to one request this way – so if the contents of the directory changes while a user is browsing they will still have the latest. (You’d have to change the JS to use some type of setInterval though). To be honest this was more of a proof of concept than anything else. Also, I didn’t use json_encode because all I wanted was an array with the first property of an object, I’d heard that json_encode tends to make normal arrays into associative arrays (~objects) which I didn’t want…

If you’re using a library I know a few of them have their own JSON request methods, so you can just swap out ‘getJSON’ for that…

]]>
By: Antoine Leclair https://j11y.io/snippets/preloading-images-from-a-directory/#comment-398 Wed, 07 Jan 2009 04:45:02 +0000 https://j11y.io/?p=374#comment-398 Nice. We don’t see too much of those “pure JavaScript” blog posts since libraries became so popular.

I’m not sure to understand the reason why you went through so much trouble though.

Couldn’t you have simply passed a JSON object from PHP (with json_encode()) containing the filenames and use your preload function to load the images?

I am not a JavaScript expert, I may simply be missing the point here.

]]>
By: Vasili https://j11y.io/snippets/preloading-images-from-a-directory/#comment-397 Wed, 07 Jan 2009 00:51:30 +0000 https://j11y.io/?p=374#comment-397 You shouldn’t use count($dirContents) in the for loop because it gets called every time the loop is run. 😉

]]>