Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Feb 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts
    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
    Last edited by brandonH; 02-01-2007 at 11:37 PM.
    I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.

  • #3
    New Coder
    Join Date
    Feb 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts
    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.

  • #5
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts
    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.

  • #6
    New Coder
    Join Date
    Feb 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •