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 4 of 4
  1. #1
    New Coder
    Join Date
    Apr 2009
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    add onload to run js when page fully loaded

    Im running js to do some color coding in a table, i tried adding the onload but its not working, can someone take a look ? I need this to run when the entire page is fully loaded and not before.

    THE FUNCTION
    Code:
    var table1_column_settings = [
        [32,40],          // ito score - First column values more than 5 will be red, 2-5 will be yellow, less than 2 will be green
        [11,12],          // schedule
        [14,15],           // cost
        [11,12],	      // execution
        [9,10],	      // commercial
        [41,50]          // total
        ];
    
    
    // Loop through all cells in a table, coloring each one according to column-specific thresholds
    function color_cells(table, column_settings)
    {
        // How many rows are in the table?
        var num_rows = table.rows.length;
        
        // Loop through all rows.
        for (var r = 0; r < num_rows; r++)
        {
            // Get a reference to the current row.
            var this_row = table.rows[r];
            // How many cells are in this row?
            var num_cells = this_row.cells.length;
            
            // Loop through all cells in this row.
            for (var c = 0; c < num_cells; c++)
            {
                // Get a reference to the cell.
                var this_cell = this_row.cells[c];
                
                // Get the numerical value of the first (and only) node inside the cell.
                var cell_value = parseFloat(this_cell.firstChild.nodeValue);
                // If it's a number (literally if it's not not not-a-number)
                if (!isNaN(cell_value))
                {
                    // Apply the color according to the thresholds for this column number (c).
                    if (cell_value > column_settings[c][1]) this_cell.style.backgroundColor= "red";
                    else if (cell_value > column_settings[c][0]) this_cell.style.backgroundColor= "yellow";
                    else this_cell.style.backgroundColor = "green";
                }
            }
        }
    }
    EXECUTE THE FUNCTION
    Code:
    </TBODY>
    </table>
    
    <script> color_cells(document.getElementById("table1"), table1_column_settings);</script>
    
    //I TRIED LIKE THIS <script> window.onload=color_cells(document.getElementById("table1"), table1_column_settings);</script> AND IT DIDNT WORK

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    window.onload is supposed to be assigned to a function and not to the result of a function execution (except if the function being executed returns another function).

    Code:
    window.onload = function () {
       color_cells(document.getElementById("table1"), table1_column_settings);
    };

  • #3
    New Coder
    Join Date
    Apr 2009
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts
    so then do i have to call this function some how ? or because of the onload it will auto matically execute

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    The latter


  •  

    Posting Permissions

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