...

View Full Version : How do I put this into an Array to prevent repeating code



lfedje
02-01-2007, 11:59 PM
Hi all

I am fairly new to javascript, and can't seem to get my head wrapped around working with arrays.

Here is the code that I have developed so far, however you will see the same code repeated twice. I know that an Array will solve this, but???


<script language="JavaScript" type="text/javascript">

/*********** Add onload="pointconversion()" to the Body Tag ***********/

function pointconversion() {
var y = document.getElementsByTagName('font');
for (var i=0;i<y.length;i++) {
var strlength =document.getElementsByTagName('font')[i].style.fontSize.length;
var pointsorpixels =document.getElementsByTagName('font')[i].style.fontSize.substr(strlength-2,2);
if (pointsorpixels=="pt")
{
var newsize=Math.ceil(parseFloat(document.getElementsByTagName('font')[i].style.fontSize)*1.3);
document.getElementsByTagName('font')[i].style.fontSize = newsize;
}
}

var y = document.getElementsByTagName('div');
for (var i=0;i<y.length;i++) {
var strlength =document.getElementsByTagName('div')[i].style.fontSize.length;
var pointsorpixels =document.getElementsByTagName('div')[i].style.fontSize.substr(strlength-2,2);
if (pointsorpixels=="pt")
{
var newsize=Math.ceil(parseFloat(document.getElementsByTagName('div')[i].style.fontSize)*1.3);
document.getElementsByTagName('div')[i].style.fontSize = newsize;
}
}
}

</script>

I know that it all starts with something like this, but from there I am lost.


tagtype = new Array('div','font')

The code works well the way it is, but in my opinion it looks sloppy.

What this code does is simply change all of the Font-Size's from points to pixels to handle the 120 dpi setting on some monitors.

I also call this code from the Body Tag, but perhaps there is some other way that it can be done.

If there are any other suggestions as to how I can shorten the code or improve on it, that would be appreciated as well.

Thanks, Loren

brandonH
02-02-2007, 12:15 AM
function pointconversion() {
var arr=new Array('div','font');
var j='0';
var y = document.getElementsByTagName(arr[j]);
while(j<='2'){

for (var i=0;i<y.length;i++) {
var strlength =document.getElementsByTagName(arr[j])[i].style.fontSize.length;
var pointsorpixels =document.getElementsByTagName(arr[j])[i].style.fontSize.substr(strlength-2,2);
if (pointsorpixels=="pt")
{
var newsize=Math.ceil(parseFloat(document.getElementsByTagName(arr[j])[i].style.fontSize)*1.3);
document.getElementsByTagName(arr[j])[i].style.fontSize = newsize;
}
}
j++;
y = document.getElementsByTagName(arr[j]);
}

}





this should do it...... hope its what you were looking for

lfedje
02-02-2007, 12:26 AM
WOW! I would have never expected a response so quickly.

Neat, clean and does the job perfectly.

Now I am going to take the time to understand exactly how you did it.

Thanks a ton for the help, Brandon.

Loren

brandonH
02-02-2007, 12:30 AM
if you want i can explain it, lol.....

brandonH
02-02-2007, 12:36 AM
I highlighted the areas that were added or changed to make it happen.

lfedje
02-02-2007, 12:40 AM
Thank's, but this is how I learn. lol

I will now take and attempt to do that with a similar piece of code that I have which has 8 elements in the array.

Loren



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum