PDA

View Full Version : Can you help me with JS this function?


Ryratt
05-17-2005, 07:48 PM
Somebody else wrote this and I commented the questions I have on it. Also commented the other stuff :L

function MM_preloadImages() { //v3.0
// d = document
var d=document;
if(d.images) { // if this document contains images
if(!d.MM_p) { // if MM_p doesn't exist, create it
d.MM_p=new Array(); // MM_p is an array for
}
var i; // Indexer for looping through array
var j=d.MM_p.length; // J is the length of the Array created. 0 on first run.
var a=MM_preloadImages.arguments; //create an array of the passed arguments.
for(i=0; i<a.length; i++) { // while i is less than the number of arguments
if (a[i].indexOf("#")!=0) { // Why is this checking for a #?????
d.MM_p[j]=new Image; // create new image in the document using the filename in the array
d.MM_p[j++].src=a[i]; // why is the j on this one number higher than the last one???
}
}
}
}

ANyway, I am just trying to figure out J and the #

Boskic.com
05-22-2005, 04:03 PM
Function is from Macromedia Dreamweaver snippets section (readable MM function).
Original source is function preloadImages()
{
if(document.images)
{
if(!document.imageArray) document.imageArray = new Array();
var i,j = document.imageArray.length, args = preloadImages.arguments;

for(i=0; i<args.length; i++)
{
if (args[i].indexOf("#")!=0)
{
document.imageArray[j] = new Image;
document.imageArray[j++].src = args[i];
}
}
}
}

# is to skip file name with that sign (like FF i Opera or diifferent OS wich cannot read source if that sign is in path).
j++ is automatic increcment, author of script has shortent it. Instead of document.imageArray[j].src = args[i]; j = j + 1;

Hope I helped, if something is wrong, please someone correct it.

petefs
05-23-2005, 05:36 PM
A tip, aside: if you're just preloading rollovers or something like that for your site, you can do it very easily (and without having to worry about the client having javascript enabled) by attaching the image as a background element via CSS to elements that do not have background images, and setting a large negative value for position/offset.

i.e.


#nav-1 { background: url(../images/my-rollover.jpg) no-repeat -10000em; }


If you do this for all the images needed for your rollover states, they will be preloaded but not displayed on that element. Obviously this is most useful for a fixed number of rollover images -- the javascript is handier when you'll be having a lot of different images on different pages preloaded : )