Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I reduce this code.....

Status
Not open for further replies.

girlinterrupted24

Programmer
Nov 16, 2005
35
ES
Hi guys,

This is a function I use to update image SRC's.... BUT with 20 pictures its so long... NOT only that I would like to basically say IF there is a value passed into the function for say pic1 and so on THEN update the SRC, IF there is no value passed for that one, then dont do anything with that pic num...

so sort of like this... but i dont know how to do it in javascript - i managed to my original function... but when it comes to something more complex like this im a bit lost :(

for i = 1 to 20

if pic1 is filled then
document.images["pic1"].src = "../images/property" + pic1 + tmp;
next

CURRENT FUNCTION

function Refresher(pic1,pic2,pic3,pic4,pic5,pic6,pic7,pic8,pic9,pic10,pic11,pic12,pic13,pic14,pic15,pic16,pic17,pic18,pic19,pic20) {
tmp = new Date();
tmp = "?" + tmp.getTime();

document.images["pic1"].src = "../images/property" + pic1 + tmp;
document.images["pic2"].src = "../images/property" + pic2 + tmp;
document.images["pic3"].src = "../images/property" + pic3 + tmp;
document.images["pic4"].src = "../images/property" + pic4 + tmp;
document.images["pic5"].src = "../images/property" + pic5 + tmp;
document.images["pic6"].src = "../images/property" + pic6 + tmp;
}


is this possible? many thanks your help
 
Unfortunately, you cannot miss out parameters by using double commas when calling a function in JavaScript (like you can in some languages), so when there is no value to pass, you'll still need to include a parameter. For example, you could not use:

Code:
Refresher('img1.gif', , 'img3.gif);

to skip image 2. Instead you could use something like this:

Code:
Refresher('img1.gif', '', 'img3.gif);

and then pick up on the blank string instead.

As to reducing the argument count and code size, this should work:

Code:
function Refresher() {
	for (var loop=0; loop<arguments.length; loop++) {
		if (arguments[loop] != '') {
			document.images['pic' + (loop+1)].src = '../images/property' + arguments[loop] + '?randomParam=' + new Date().getTime();
		}
	}
}

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hi Dan,

Thanks so much for this, I am just going to put it in now, but can I just ask... I kinda like to learn new things rather than just copy paste... Also quickly on your point about it would need to be '' and not with the 's - thats perfect because that is how it is :)

So, just wanted to check where I have used " like on "../images/property" You have swapped these to use single comma '

Should I use this instead for future little js functions I do? Or is there no real difference?

And then... arguments.length is this basically the total variables it has found being passed into the funcion... so each instance of the '' (or in a filled case 'information') ?

Thanks so much again for your help - SO MUCH APPRECIATED :)
 
Use of double quotes over single is a matter of personal preference. I just happen to always use single for my JS, and double for my HTML.

"arguments" is basically an array of the parameters passed into a function. Much like an array, you can check its length property, and loop around getting each individual item (parameter) from it.

You can still have named parameters, however, so in the following example, "arguments[2]" would always be the same as "z":

Code:
function someFunc(x, y, z, a, b, c) {
}

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top