I had opportunity to meet with Creole a few days ago and we were discussing some options to pursue in the Build forum. I thought I'd start here in javascript and find out methods users use to preload images. I realize that this information can likely be found in a google search, but if we can get some good insight, maybe start putting an FAQ of javascript issues for Sitepoint Forums together. What do you think?
I have a site that I have a bunch of rollovers for..
I have a preload image script as follows:
<SCRIPT LANGUAGE="JavaScript"> <!-- Begin image1 = new Image(); image1.src = "/images/image1-over.jpg"; image2 = new Image(); image2.src = "/images/image2-over.jpg"; // End --> </script>
This works just fine for NETSCAPE... it preloads it, so you can cruise over the rollovers perfectly without wait the first time you load.
However, with IE, it doesn't work the way it should, and it still takes time to load each rollover when you hover the first time. And they don't seem to cache properly and you're constantly reloading them.
I have tried several preload scripts found here; plus, some of my own. The only thing that works is the unsophisticated loading of those tiny images. The download consist of 100+ images amounting to 50+mb; and, normally completes in less than 1 minute without preload (using DSL).
The preload terminates after 6 or 7 images and seems to time-out in the middle of an image. A reload will download a few more, etc. I have used the <body onLoad=function()> approach as well as the non-function approach of JS that executes immediately with the page's loading. Getting the same results with Netscape-7 and IE-6.
I need to add: The source of the array containing the image references is a '.js' file. That doesn't seem pertinent as some of the images always load.
I would like to preload all images on a certain page, without 'knowing' which pictures are on the page. (I want to write a .js file, put in the header of all the pages)... So, I don't know what pictures are on the particular pages.
Can this be done? If so, how?
Than another question (if the above is possible)... Say, one of the 'found' pictures can't be loaded (it's not in the correct folder)... can this pictures be replaced with another picture? (for instance: pic_not_found.jpg).
the first 4 tabs are showing some images as background:clicking the tabs will trigger a fading-effect, but as long as the user hasn't watched the 4 images, the effect is "interupted" by the loadingprocess of the images. is it possible to preload those images so that they are in the browsercache or something?
default1 = new Image(); default1.src = "images/image1.gif"; changed1 = new Image(); changed1.src = "images/image1-on.gif"; default2 = new Image(); default2.src = "images/image2.gif"; changed2 = new Image(); changed2.src = "images/image2-on.gif";
And this is my rollover code:
var iName=""; function Ichange(p) { var pSrc=eval(p+ ".src"); document[iName].src = pSrc; }
But for some reason on the first mouseover it doesn't seem like the image has been preloaded. There is a delay, like it's looking for the rollover image, which should have already been preloaded. Anyone have any ideas why?
i am making a gallery and i was advised by philip m to preload my images before hand, so that the gallery runs smooth. i have done so but in a different way to what ive seen on some of the other posts on this forum and i was just wondering if this was practical javascript or am i over doing it.bascially, the images are external hrefs loaded into place holders.the external hrefs are stored as strings in an array.
so the loop runs for however many image hrefs are in the array.so the first iteration of the loop, an image is created and is given the href of whateever value the loop is of the array.then for each iteration of the loop, the var img, is overwritten with the next href in the array.so technically, iam not creating as many images as there are in the array, i am just creating 1 then overwriting it, but still loading the image data in to the browser cache.
I have some images 5 or 6 that are 800 x 300 i use in my website banner.The start loading but when number one is 1/3 of the way teh next one starts then the next, it looks real messy nee some way to preload them ... like black square or something .code i used below
<script language="JavaScript"> // configuration structure var A_TPL = {[code].....
I want to create a preload for images but do not want to have to specify every single image file name.Is there a way to just specify a folder and all images in ti will be preloaded?
i want all of the images to preload, but i want to do it in such a way that i don't have to list all of the images a second time.. I want it to somehow grab all the images from the array and load them. is there a simple or relatively easy way of doing that?
I have a site that uses image navigation with a CSS rollover. The images flicker for about a second while the rollover images load. I'd like to preload the images with JS. Now, they're about 10 images in one directory, is there a way to just preload all the contents of the directory?
I am using the below preload images script and It seems to not be working... PHP Code:
<script> function doPreload() {
var the_images = new Array('/images/pd2.gif','/images/explore2.gif', '/images/games2.gif','/images/shops2.gif','/images/chat2.gif', '/images/mail2.gif','/images/help2.gif','/images/login2.gif', '/images/logout2.gif'); preloadImages(the_images); }
I have a rolling image script that works fine except that the images do not seem to be preloading properly. The images are still being pulled on every interval from the server according to the logs...is it because I am using the document.write method? Is that forcing a refresh? Code:
i am working on a facebook like photo viewer. I have used javascript to declare an array of about 200 images. I have the following approach to pre-load the images.I preload all the images when the page loads:
The problem is the page tries to load all the images when opened. And when the user clicks "next" to view other photos, the other photo is still loading where as some are already loaded.I would like the code to be able to load the photo that the user is currently viewing as the user gets to that picture, before the page tries to finish loading all the photos.In other words, if the user wants to view a particular photo directly, that photo should get the priority of loading quickly.
I have built a function that allows users to select a color tile and trim option for a cooking range. Here is a code snippet showing the function:
var color, trim, description; function displayRange(selectColor, selectTrim, showDes) { var rangeName; if (selectColor != '') color = selectColor; if (selectTrim != '') trim = selectTrim; rangeName = "images/cluny1400" + color; if (trim == 'chrome') rangeName += "_chrome"; rangeName += ".jpg";
var rangeDescription; if (showDes != '') description = showDes; rangeDescription = description; document.getElementById('rangePict').src = rangeName; document.getElementById('rangeDes').innerHTML = rangeDescription; }
Based on the users selections the range image in the selected color and trim is loaded into the interface. My problem is this:
The selected image is made up of three elements, based on the users selection, color and trim and the .jpg extension. This function works well.
However, I cannot work out how to use the image object to preload the images (of which there will be over 34). Normally you would assing a var to hold a new instance of the image object and then assign the src property to the image url. But how do I do that when the var rangeName is composed of three elements. I cannot use a simple var and then call that var in the function.
I have this code snippet, that illustrates my problem:
// create function to preload images // eliminate older browsers by checking for image object support function preload() { if (document.images) { cluny1400au = new Image(478, 314); cluny1400au.src = "images/cluny1400au_fw.jpg"; cluny1400au_fw_chrome = new Image(478, 314); cluny1400au_fw_chrome.src = "images/cluny1400au_fw_chrome.jpg"; } }
The vars cluny1400au and cluny1400au_fw_chrome would be fine if I could use them in the function to call the images, but I don't think this will work as I believe that you have to use the same var name in the function as in the image object.
Can anyone tell me how the cluny1400au = new Image(478, 314); and cluny1400au_fw_chrome.src = "images/cluny1400au_fw_chrome.jpg"; should be constructed to work with the function.
Here is an html snippet from the page to aid clarity:
Ever had the problem that when creating mouseover buttons using CSS and Javascript, it takes a little while to load the second image? This script will automatically loop through all loaded stylesheets and will preload them into your browser, so no need to create an image-list by hand.. Code:
Am using jquery and javascript to load images.Everthing is working fine, but I would like the user to be taken to a webpage for each of the images below.
Code: function PreloadImg(){ $.ImagePreload("/images/slash2.jpg");
WANT: Client to save an image to disk pressing a "download" button. The image name is dynamic, from JS code on another part of the page
DID: Read somewhere I can use document.execCommand("SaveAs") to do it This solves it for MSIE, at least. My code is below
PROBLEM: What actually happens with MSIE is: 1) A window pops up promting me to save as.
2) The filename is correct, but the saveas type shown in the box is limited to "html" and "txt"
3) The "image" that is saved is not an image, but an HTML file containing the link to the image!! This is NOT what I want!
PLEASE help. How do I get MSIE to save the actual image instead? PS With netscape, instead of the saveas dialogue, a window opens with the image displayed. By clicking <CTRL-S> I can save the actual image.
Is there anyway of disabling images in javascript, so that the alternative text appears instead of the images? I'm trying to do a text-only version of my site.
I have some image data in a Javascript variable, and I want to display it. I have a technique which works in Firefox but not in Internet Explorer :
<script> var testImage = <image data as a string> </script>
<img src="javascript:testImage"></img>
I understand that this should work in Javascript 1.2 and above, which supports null characters in strings. Does anyone know why this doesn't work in Internet Explorer?
Let's say I have a Javascript function that loops over and over. In that function i have it alternating images on a button this way:
if(var==0){ var myHTML = "<input type='button' style='background-image: url(grnbutt.jpg);etc. etc.'>"; var outq = document.getElementById("qbutt"); outq.innerHTML = myHTML; } else{ var myHTML = "<input type='button' style='background-image: url(grnbuttdark.jpg);etc. etc.'>"; var outq = document.getElementById("qbutt"); outq.innerHTML = myHTML; }
This works great using Firefox, and works with Internet Explorer also EXCEPT if its set to "Check for newer versions of stored pages: Every visit to the page". With that setting, IE will download those 2 images from the server every single time through the loop. So then I tried putting both images on the same jpg and then moving the image:
if(var==0){ var elem = document.getElementById("grn"); elem.style.backgroundPosition = "-100px 0"; } else{ var elem = document.getElementById("grn"); elem.style.backgroundPosition = "center"; }
This also works, but also, constantly downloads new images when using IE with the above setting. Microsoft calls this a "feature". They then offer a fix for this "feature" that... doesn't work. Preloading doesn't work. Loading the 2 images in a <divset to invisible doesn't work. I've tried suggestions of creating php files that tell the browser that the images don't expire for years. That didn't work for me. Many people have also suggested that there is no way around it... it can't be fixed until Microsoft fixes it.
So, is there any javascript way to loop 2 images on a button, without Internet Explorer constantly downloading those 2 images over and over, when its set to "Check for newer versions of stored pages: Every visit to the page"?