Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-01-2007, 10:59 PM   PM User | #1
lfedje
New Coder

 
Join Date: Feb 2007
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
lfedje is an unknown quantity at this point
How do I put this into an Array to prevent repeating code

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???

Code:
<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.

Code:
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
lfedje is offline   Reply With Quote
Old 02-01-2007, 11:15 PM   PM User | #2
brandonH
Regular Coder

 
Join Date: Oct 2003
Location: on a ship
Posts: 574
Thanks: 1
Thanked 6 Times in 5 Posts
brandonH is on a distinguished road
Code:
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
__________________
I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.

Last edited by brandonH; 02-01-2007 at 11:37 PM..
brandonH is offline   Reply With Quote
Old 02-01-2007, 11:26 PM   PM User | #3
lfedje
New Coder

 
Join Date: Feb 2007
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
lfedje is an unknown quantity at this point
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
lfedje is offline   Reply With Quote
Old 02-01-2007, 11:30 PM   PM User | #4
brandonH
Regular Coder

 
Join Date: Oct 2003
Location: on a ship
Posts: 574
Thanks: 1
Thanked 6 Times in 5 Posts
brandonH is on a distinguished road
if you want i can explain it, lol.....
__________________
I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.
brandonH is offline   Reply With Quote
Old 02-01-2007, 11:36 PM   PM User | #5
brandonH
Regular Coder

 
Join Date: Oct 2003
Location: on a ship
Posts: 574
Thanks: 1
Thanked 6 Times in 5 Posts
brandonH is on a distinguished road
I highlighted the areas that were added or changed to make it happen.
__________________
I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.
brandonH is offline   Reply With Quote
Old 02-01-2007, 11:40 PM   PM User | #6
lfedje
New Coder

 
Join Date: Feb 2007
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
lfedje is an unknown quantity at this point
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
lfedje is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:17 AM.


Advertisement
Log in to turn off these ads.