SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2007
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help interpreting

    Can someone help me figure out what this means in the red font. I have some of it figured out (thanks to changing the table borders so I could see what fell where.) I just don't want this to produce a two column table and I also want to change the layout, I want to move the total savings of all companys which has been given the id of 0 but when I tried what I thought might work in blue font it did not work. Usually I can do trial and error editing to figure out how it works but not this one. I need a translation on what's in red. I think. Or maybe I need someone to tell me what part of this code is creating a two column table for each of the companies.

    <script>


    var company=new Array()

    // delete or add new companys like this:
    // company[x]="company|hours of energy saved per year in grams hours times 950 grams each hour"
    company[0]="<strong>grams of CO2 saved by All Earth and Sky Certified companies</strong>|11096000"
    company[1]="<strong>grams of CO2 saved by Sun Country Farms</strong>|4161000"
    company[2]="<strong>grams of CO2 saved by Rathwell</strong>|6935000"
    company[3]="<strong>grams of CO2 saved by test</strong>|77777777777"


    // font
    var thisfont="Arial"

    // font-size
    var thissize=10

    // font-color
    var thiscolor="black"


    // font of title
    var titlefont="Arial"

    // font-size of title
    var titlesize=10

    // font-color
    var titlecolor="#000000"


    // backgroundcolor title
    var titlebg="#ffffff"

    // backgroundcolor savings counter
    var bg="#ffffff"


    // do not edit below this line
    var t = 0
    var enterTime = new Date()
    var enterTime = Math.floor(enterTime.getTime()/1000)
    var secyear=31536000
    var rowflag=false
    var i_companys=0

    if (document.getElementById) {
    initdisplay()
    }

    function initdisplay() {
    document.write('<TABLE BORDER=5 CELLSPACING=0 CELLPADDING=0 bgcolor="'+bg+'">')
    document.write('<tr valign=top>')
    document.write('<td colspan=4 style="font-family:'+titlefont+';font-size:'+titlesize+'pt;color:'+titlecolor+';background-color:'+titlebg+'" id="titlecell" align=center>')
    document.write(' Realtime C02 Savingscounter: grams of Co2 saved within the last 0 seconds')
    document.write('</td></tr>')

    document.write('<tr valign=top>')
    document.write('<td colspan=2>')
    document.write(' ')
    document.write('</td></tr>')
    document.write('<tr valign=top>')
    document.write('<td><TABLE BORDER=20 CELLSPACING=0 CELLPADDING=0>')
    for (i=0; i<=(company.length)/2; i++) {
    var content=company[i].split("|")
    document.write('<tr valign=top>')
    document.write('<td align=right id="cell'+i+'" style="font-family:'+thisfont+';font-size:'+thissize+'pt;color:'+thiscolor+'">')
    document.write('empty')
    document.write('</td>')
    document.write('<td>')
    document.write(' ')
    document.write('</td>')
    document.write('<td align=left style="font-family:'+thisfont+';font-size:'+thissize+'pt;color:'+thiscolor+'">')
    document.write(content[0]+" ")
    document.write('</td>')
    document.write('</tr>')
    i_companys++
    }
    document.write('</TABLE></td>')
    document.write('<td> </td>')
    document.write('<td><TABLE BORDER=10 CELLSPACING=0 CELLPADDING=0>')
    for (i=i_companys; i<=company.length-1; i++) {
    var content=company[i].split("|")
    document.write('<tr valign=top>')
    document.write('<td align=right id="cell'+i+'" style="font-family:'+thisfont+';font-size:'+thissize+'pt;color:'+thiscolor+'">')
    document.write('empty')
    document.write('</td>')
    document.write('<td>')
    document.write(' ')
    document.write('</td>')
    document.write('<td align=left style="font-family:'+thisfont+';font-size:'+thissize+'pt;color:'+thiscolor+'">')
    document.write(content[0]+" ")
    document.write('</td>')
    document.write('</tr>')
    }
    document.write('</TABLE></td></TABLE>')
    }

    function docounter() {
    var now = new Date()
    var now=Math.floor((now.getTime()/1000))
    var hereSince = now - enterTime
    for (i=0; i<=company.length-1; i++) {
    var content=company[i].split("|")
    content[1]=Math.floor(content[1]/secyear*hereSince)
    document.getElementById('cell'+i).innerHTML=content[1]
    document.getElementById('titlecell').innerHTML=" In the "+hereSince+" seconds since you arrived at our site the Earth and Sky Friendly Certification Program has saved "+company[0]+" from entering the air"
    }
    var setT = setTimeout("docounter()",1000)
    }


    function showNr(zahlProSec,hereSi,gifNa) {

    var zahlSince = ""+Math.floor(hereSi*zahlProSec)
    var rechtsPos = 9 - zahlSince.length-1

    for (i=0; i<= zahlSince.length-1; i++) {
    document.images[gifNa+(i+rechtsPos)].src = "z"+zahlSince.charAt(i)+".gif"
    }
    }

    if (document.getElementById) {
    window.onload=docounter
    }
    </script

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    These links will explain what for loops and the split function do:
    http://developer.mozilla.org/en/docs...:for_Statement
    http://developer.mozilla.org/en/docs...s:String:split

    They are looping through the "company" array (defined right at the beginning of the script). Each one of the things in the array (each company) is an array element. The for statement loops through the array and does something with each element. In this case, it uses the split function to split the element by the pipe (|) and so each element is split into two bits: some HTML (in the <strong> tags) and a number. The HTML is displayed using the "write" method of the document object (document.write) on this line:
    Code:
     		document.write(content[0]+" ")
    Upon each iteration of the loop, "content" becomes an array of the two bits that the "split" method made from the current "company" element. Then content[0] is the HTML bit. content[1], which is the number, appears not to be used.

    I don't know what you mean about the layout, it isn't very clear. Also, I suspect you are unaware of this, but the use of document.write is discouraged. Since you're using document.write, I also suspect you are putting Javascript in the BODY of your HTML document, which is also frowned upon. The use of tables for layout and most of the attributes you are using (e.g. "border", "cellpadding", "align", "valign" "bgcolor") are unnecessary and should not be used. Instead, you should use CSS. This also goes for what you have put in the "style" attributes. Finally, from what I can see, you don't need javascript at all for the table display. Instead, this could be done on the server side, using a language like PHP, Ruby or ASP.

    And next time you post some code, please use the CODE tags so it is in nice, readable code boxes like the one I used above.


Bookmarks

Posting Permissions

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