/    Sign up×
Community /Pin to ProfileBookmark

JS Image rollovers very slow

I have a relatively simple set of links that change an image and text as you roll over a set of links. Everything is working fine however the image rollover seems to respond very slow – as you roll over the links the text updates instantly however the image can take a few seconds to update. I thought preloading the images would fix this problem but it’s still there.

There are two things I’m worried about:
1) It seems to me that I’m not actually caching the image (only the image url) – when I assign the src element isn’t it still the masthead img’s job to go get the image across the internet? Maybe this works because (we are counting on) the browser to have already cached the image data? I don’t really understand how this works.
2) My images are being generated dynamically by the server (../images/Image.X.aspx) – this page returns the appropriate image outta the database and sets the correct image size. Can images generated by the server be cached? If not this might be counteracting the benefit of preloading the images and causing my problem?

Example code follows:

[CODE]var imageCount=12;
var oImages = new Array();
function PreloadImages()
{
for (i=1; i <= imageCount; i++)
{
oImages[i] = new Image();
oImages[i].src = “../images/Image.” + i + “.aspx”;
}
preloadComplete = true;
}
function showImage(SelectedIndex)
{
if (preloadComplete == true )
{
// change masthead image
var oMastheadImage = document.getElementById(‘MastheadImage’);
oMastheadImage.src = oImages[SelectedIndex].src;
oMastheadImage.alt = GetTitleText(SelectedIndex);
/* change masthead text */
var oMastheadText = document.getElementById(‘MastheadText’);
oMastheadText.innerHTML = GetTitleText(SelectedIndex);
}
}[/CODE]

Here is an example of some of my links:
<a onmouseover=”showImage(1); return true;” href=’Link1.aspx’>Link 1</a>
<a onmouseover=”showImage(2); return true;” href=’Link2.aspx’>Link 2</a>

Thanks in advance for your help,
J. Shane Kunkle
[email][email protected][/email]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@konithomimoNov 09.2005 — <i>
</i>&lt;script type="text/javascript"&gt;
var imageCount=12;
var oImages = new Array();

function showImage(SelectedIndex)
{


for (var i=0; i &lt;= (imageCount-1); i++)
{
oImages[i] = new Image();
oImages[i].src = "../images/Image." + (i-1) + ".aspx";
}

// change masthead image
document.getElementById('MastheadImage').src = oImages[SelectedIndex].src;
document.getElementById('MastheadImage').alt = GetTitleText(SelectedIndex);
/* change masthead text */
document.getElementById('MastheadText'.innerHTML = GetTitleText(SelectedIndex);
}
&lt;/script&gt;


you had for (i=1 . . .

arrays start at 0
Copy linkTweet thisAlerts:
@J__Shane_KunkleauthorNov 09.2005 — Thanks for your response - i might not have been clear in my initial post so i have rewritten the functions (included at the end of this post) so they might be a little clearer based on your input.

However, My question is a performance based question - i do not believe i have any problems with runtime errors - this code is working fine, my question is why do the images that are changed in onmouseover take so much longer to change than the text that is changed in the same function? Is my image caching/preload not working? Is there an inherit flaw in my design (image caching won't work on the client with images served by an aspx page, etc)?

Any ideas? Thanks again!


Note: the image id's etc are 1 based (db primary keys) - thus the looping staring at 1 in my old code and the “(i – 1)” calculations in my new code below.

[CODE]function PreloadImages()
{
for (i=0; i < imageCount; i++)
{
oImages[i] = new Image();
oImages[i].src = "../images/Image." + (i + 1) + ".aspx";
}
preloadComplete = true;
}

function showImage(SelectedIndex)
{
if (preloadComplete == true )
{
// change masthead image
var oMastheadImage = document.getElementById('MastheadImage');
oMastheadImage.src = oImages[SelectedIndex - 1].src;
oMastheadImage.alt = GetTitleText[SelectedIndex - 1];
/* change masthead text */
var oMastheadText = document.getElementById('MastheadText');
oMastheadText.innerHTML = GetTitleText[SelectedIndex - 1];
}
}[/CODE]
Copy linkTweet thisAlerts:
@konithomimoNov 09.2005 — The reason why I put it all into one function is because you are trying to call oImages in oImages, but showImage is populated in PreloadImages. The variable oImages is global, but the values put into it via PreLoadImages are not meant to be accessed from another function. Also, creating more variables and assigning them values and things is not needed since it just makes more for the comp to do. The other problem might just be the images that you are trying to use.
×

Success!

Help @J__Shane_Kunkle spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...