...

View Full Version : Pre-Load Images



moogle1979
01-28-2009, 10:51 PM
Is there a way to Pre-Load the images on the site without having to add new images to the array each time one is added. Basically what I need is the code automatically searches the images folder and creates an array to Pre-Load all the images in the image folder so when it goes to another page on the site it is automatically loaded in the cache.

Philip M
01-29-2009, 09:07 AM
Is there a way to Pre-Load the images on the site without having to add new images to the array each time one is added. Basically what I need is the code automatically searches the images folder and creates an array to Pre-Load all the images in the image folder so when it goes to another page on the site it is automatically loaded in the cache.


JavaScript is purely a client-side language, and has no capability to read from or write to a file (except a cookie), access a database or the client's operating system. So this can be done only with server-side scripting.

freedom_razor
01-29-2009, 12:10 PM
You may find this tutorial (http://www.javascriptkit.com/javatutors/externalphp.shtml) useful for your purpose.

moogle1979
01-29-2009, 03:51 PM
I have never heard of JavaScript calling a php file.

freedom_razor
01-29-2009, 03:59 PM
As long as output from php file is valid Javascript code, it works. Try that tutorial, it is actually quite close to what you want to achieve, even if its mixed php with javascript.

moogle1979
01-29-2009, 04:04 PM
I was thinking too if I could just somehow pull the images off with php into an array and read that array with JavaScript to Pre-Load the images, thanks I will try it out

itsallkizza
01-29-2009, 05:00 PM
Here I wrote this for you.

Test: http://buildyourownbagel.com/test/get_images_in_dir.php?dir=images/

get_images_in_dir.php:


<?php
$extensions = array("jpg","gif","png");
$all_files = scandir($_GET["dir"]);
$images = array();
for ($i=0;$i<count($all_files);$i++)
{
$is_image = false;
for ($x=0;$x<count($extensions);$x++)
{
if (substr_count(strtolower($all_files[$i]),$extensions[$x]) != 0)
{
$is_image = true;
break;
}
}
if ($is_image) array_push($images,$all_files[$i]);
}
echo("[");
if (count($images) != 0) echo('"'.implode('","',$images).'"');
echo("]");
?>


All you have to do is send a synchronous xmlhttp request to "get_images_in_dir.php" with the directory in question attached as a variable. It will return a string which you can eval into a JS array.

moogle1979
01-29-2009, 09:37 PM
Thanks, I will probably write a js file with an AJAX function and a few arguments in the function so I can call that function from another page and use the arguments to determine how to run the function.

like



function makeRequest(page) {
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}
else {
if (window.ActiveXObject) {
try {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) { }
}
}

if (xhr) {
xhr.onreadystatechange = showContents;
xhr.open("GET", page, true);
xhr.send(null);
}
else {
document.getElementById("updateArea").innerHTML = "Sorry, but I couldn't create an XMLHttpRequest";
}
}



Then I can use the page variable to change the page requested per what I need



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum