/    Sign up×
Community /Pin to ProfileBookmark

Preloading images is not working

Hello everyone, it’s my first pose in here.
I have a preload script here, but I don’t know why it is not working. Here is the code:

In page.js:

[code]
function Preloader() {
this.images=[];

this.elemRef=document.getElementById(arguments[0]);

for(var i=0, len=arguments.length; i<len; i++) {
this.images[i]=new Image();
this.images[i].src=arguments[i];
}
}

function Change(image, item) {
item.src = image;
}[/code]

In index.html (I declared my script in the header), inside BODY:

[code]
<script type=”text/javascript”>
Preloader(“menu1_o.gif”, “menu2_o.gif”);
</script>

… some html here …

<!– menu –>
<img src=”menu1.gif” onmouseover=”Change(‘menu1_o.gif’, this);” onmouseout=”Change(‘menu1.gif’, this);” />
<img src=”menu1.gif” onmouseover=”Change(‘menu2_o.gif’, this);” onmouseout=”Change(‘menu2.gif’, this);” />
[/code]

The page and events work, except the preloader. I still have the delay when the mouse goes over the menu, because it is only downloading the over image when the event is triggered.
What am I doing wrong?

Thanks in advance,
Fergo

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@toicontienMar 31.2008 — I'm not sure if using the "this.images" syntax in a function will work with how you are using it. Try this instead:
var Preload = {
images : [],
isSupported : !!document.createElement,

Add : function() {
if (!this.isSupported)
return;

<i> </i>var i = 0;
<i> </i>var end = arguments.length;
<i> </i>var img = 0;
<i> </i>while(i &lt; end) {
<i> </i> img = this.images.push( document.createElement("img") ) - 1;
<i> </i> this.images[img].src = arguments[i];
<i> </i> i++;
<i> </i>}
}
};

Usage:
Preload.Add("img1.gif", "img2.gif", ... "imgN.gif");
×

Success!

Help @Fergo 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,
)...