/    Sign up×
Community /Pin to ProfileBookmark

How to trim down this heavy javascript

Second question of the day. Sorry to be greedy. I have some javascript which repeats large blocks of code with a few subtle difference. I was wondering how to trim it. Ideally I would like to refernce as much as possible from an external .js file. But I am not sure which parts can be cut out. The code is in the body of the html.

The page has about 50 different images and each leads to a chunk of code which is an onclick event…The middle part of each chunck does not vary.

Each section looks like this (i have changed the bits that vary to “VARIES”

[CODE]<TD vAlign=top width=45><A href=”#topanchor”
onclick=”
VARIES.src=’http://hardyart.co.uk/frame/VARIES.jpg’;
document.getElementById(‘VARIES’).style.backgroundImage =
‘url(‘http://hardyart.co.uk/frame/VARIES.jpg’)’;
document.getElementById(‘VARIES’).style.backgroundImage =
‘url(‘http://hardyart.co.uk/frame/VARIES.jpg’)’;
document.getElementById(‘VARIES’).style.backgroundImage =
‘url(‘http://hardyart.co.uk/frame/VARIES.jpg’)’;
document.getElementById(‘VARIES’).style.backgroundImage =
‘url(‘http://hardyart.co.uk/frame/VARIES.jpg’)’;
document.getElementById(‘VARIES’).style.backgroundImage =
‘url(‘http://hardyart.co.uk/frame/VARIES.jpg’)’;
document.getElementById(‘VARIES’).style.backgroundImage =
‘url(‘http://hardyart.co.uk/frame/VARIES.jpg’)’;
document.getElementById(‘VARIES’).style.backgroundImage =
‘url(‘http://hardyart.co.uk/frame/VARIES.jpg’)’;
document.getElementById(‘VARIES’).style.backgroundImage =
‘url(‘http://hardyart.co.uk/frame/VARIES.jpg’)’;
document.getElementById(‘VARIES’).style.backgroundImage =
‘url(‘http://hardyart.co.uk/frame/VARIES’)’;
document.getElementById(‘VARIES’).style.backgroundImage =
‘url(‘http://hardyart.co.uk/frame/VARIES.jpg’)’;
document.getElementById(‘VARIES’).style.backgroundImage =
‘url(‘http://hardyart.co.uk/frame/VARIES.jpg’)’;
frameselected = ‘VARIES’; frametitle = ‘VARIES’; frameprice = VARIES; framethick = VARIES;
THIS PART DOES NOT VARY check();
document.calc2.Title.value = ‘Frame for print ‘ + ebaytitle;
document.calc2.Print.value = ‘£’ + printprice;
document.calc2.Total.value = ‘(print and frame) £’ + ((Math.round((calcaddframe(setup,backbrdprice,barrierbrdprice,glassprice,innermprice,framedarea,outermprice,framethick,borderwidth,borderheight,frameprice,slipthick,picturewidth,pictureheight,slipprice)+printprice)/5)*5)-0.01);
document.calc2.Link.value = ‘http://cgi.ebay.co.uk/ws/eBayISAPI.dll?ViewItem&item=’ + testmeout;
document.calc2.Frame.value = frametitle + ‘ (‘ + frameselected + ‘)’;
document.calc2.Mount1.value = ‘Outer Mount: ‘ + outermtitle + ‘ (‘ + outermselected + ‘)’;
document.calc2.Mount2.value = ‘Inner Mount: ‘ + innermtitle + ‘ (‘ + innermselected + ‘)’;
document.calc2.Slip.value = sliptitle + ‘ (‘ + slipselected + ‘)’;
document.calc3.D.value = ‘eBay Item Number: ‘ +testmeout; document.calcprice.p.value = ‘Print only: £’ + printprice + ‘ ; Framed print: £’ + ((Math.round((calcaddframe(setup,backbrdprice,barrierbrdprice,glassprice,innermprice,framedarea,outermprice,framethick,borderwidth,borderheight,frameprice,slipthick,picturewidth,pictureheight,slipprice)+printprice)/5)*5)-0.01);”> THEN IT VARIES AGAIN
<IMG name=imagef1 src=”http://hardyart.co.uk/frame/VARIES.jpg” Title=”VARIES” border=0></A></TD>[/CODE]

Cheers

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

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