That is called an "anonymous" function. It has no name.
function () {}
That is an anonymous function.
function () {}();
That is an anonymous function that is being called immediately. However, convention is to wrap the function definition in parenthesis as well so that you know that this is a function that is going to be executed immediately. So you end up with this:
(function () {})();
The purpose in this case is to create a function scope to store variables in so that the code doesn't contaminate the global scope with variables that might some day conflict with other libraries, etc. And since I want that code to execute immediately, putting it all in an anonymous function that executes immediately does the trick.
Also I am generating that web page from php script and its content will be dependent on concrete puzzle.
That means puzzle dimensions are not always 50x50 so can I somehow pass arguments to that function to set those global variables?
If we want to put the JavaScript in a separate file but still pass it some dynamic PHP-generated values, then there are a few options:
1. We could create a single global variable that contains an object. This acts as a sort of namespace. A good approach, but I don't think we need to go that route just yet.
2. You could keep the JavaScript in the PHP file and just use PHP where those variables are declared:
var g_columns = <?php echo cols; ?>;
But this doesn't support clean separation of content and behavior, so I don't like this option.
3. You could modify your server to run .js files through the PHP processor and then put the JavaScript code with some PHP (like #2) in the .js file. This is a better separation of layers, but I don't like the idea of having .js files running through the PHP processor, as that can affect (or be affected by) browser caching.
4. Modify the JavaScript code to determine the number of columns/rows from the DOM. This is the approach I would take. For example, you can determine the number of rows by counting the number of <tr> elements in the puzzle_table, and the number of columns by counting the number of <td> elements in the puzzle_table and then dividing that by the number of rows. For example:
g_rows = puzzle_table.getElementsByTagName('tr').length,
g_columns = puzzle_table.getElementsByTagName('td').length / g_rows;
This 4th option is the one I would chose. There are several ways to skin that cat, but this option allows you to keep the JavaScript in it's own file, and removes the need to pass any variables from PHP to JavaScript. :)
but still in IE speed is very poor.
Really? I found the speed to be nearly identical in IE7, Firefox 3.6, and Chrome. What version of IE are you using?
One thing you might try is setting the className on the TD instead of setting the background property of the style. For example:
//target.style.background = (e.button == 2 ? "red" : "blue");
target.className = (e.button == 2 ? "rmb" : "lmb");
Then in your CSS, add the following just after the #puzzle_table td rules:
#puzzle_table td.lmb { /* lmb = left mouse button */
background: blue;
}
#puzzle_table td.rmb { /* rmb = right mouse button */
background: red;
}
Generally speaking, setting the class name is slightly faster than setting properties of the style object.