/    Sign up×
Community /Pin to ProfileBookmark

Javascript – How do you defer graphic loading?

About the script below, I think that when I add more graphic file names to the array, it takes longer to load the page. So is there a strategy I can take to still display my graphics randomly and have my page load fast?

Code is from the first page of [url]www.stationisland.com.[/url]

<SCRIPT LANGUAGE=”JavaScript”>

<!– Begin
var rand1 = 0;
var useRand = 0;

images = new Array;
images[01] = new Image();
images[01].src = “blueHand.jpg”;
images[02] = new Image();
images[02].src = “wallCloud.jpg”;

function swapPic() {
var imgnum = images.length – 1;
do {
var randnum = Math.random();
rand1 = Math.round((imgnum – 1) * randnum) + 1;
} while (rand1 == useRand);
useRand = rand1;
document.randimg.src = images[useRand].src;
}
images = new Array;
images[01] = new Image();
images[01].src = “blueHand.jpg”;
images[02] = new Image();
images[02].src = “wallCloud.jpg”;
images[03] = new Image();
images[03].src = “lroom1.jpg”;
images[04] = new Image();
images[04].src = “newHousePaint.jpg”;
images[05] = new Image();
images[05].src = “nrb.jpg”;
images[06] = new Image();
images[06].src = “forest.jpg”;
images[07] = new Image();
images[07].src = “rootBody.jpg”;
images[08] = new Image();
images[08].src = “wind.jpg”;
images[09] = new Image();
images[09].src = “prb.jpg”;
// End –>
</script>
</head>

<body OnLoad=”swapPic()”

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@TageOct 11.2004 — I'm guessing you don't care about the preload functionality of the script. That is what makes the page take longer to load, and its main use is for rollover images. There were some errors and needless coding in that script. So...try using this script on your page instead...&lt;script type="text/javascript"&gt;
&lt;!--
imgs = new Array();
imgs[0] = "blueHand.jpg";
imgs[1] = "wallCloud.jpg";
imgs[2] = "lroom1.jpg";
imgs[3] = "newHousePaint.jpg";
imgs[4] = "nrb.jpg";
imgs[5] = "forest.jpg";
imgs[6] = "rootBody.jpg";
imgs[7] = "wind.jpg";
imgs[8] = "prb.jpg";

function swapPic() {
document.getElementById("randimg").src = imgs[Math.floor(Math.random()*imgs.length)];
}
//--&gt;
&lt;/script&gt;
&lt;/head&gt;
&lt;body onLoad="swapPic()"&gt;
&lt;img src="" alt="theImage" id="randimg"&gt;
&lt;/body&gt;
&lt;/html&gt;
×

Success!

Help @ccp1er 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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...