Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

JavaScript Tips

Here are some more things I have learnt whilst writing a free game:

1) the 'cloneNode' function does not copy event listeners such as 'onclick'

2) you can use 'getElementsByTagName' on any HTML element whereas 'getElementById' only works on the 'document' node

  • you can pass parameters to functions that do not take parameters. For example:

func() { }
func(1, 'a', 7);

You can get at the parameters with the 'arguments' object. You can also pass more paramters to a function than it needs

3) one way to delete all the elements of an array is to use 'arr.splice(0, arr.length)'

4) you can use the return values of 'splice' to get a range of elements from an array

5) you can get the new length of the array from the return value of 'push()'

6) you can test if an array has a value with a given key with 'if (key in arr)'

7) one way to convert something to a string is to use 'String(something)'

8) one way to convert numbers to different bases is by giving the base as a parameter of 'toString()'

9) you do not get an error if you 'delete' a property of an object and the property does not exist

But Internet Explorer 6 and 7 give an error if I delete a property I added to a button

10) the 'typeof' of an array is 'object'. I thought it might be 'array'

11) a function inside another function, used as a closure, keeps the values of variables in the outer function at the time that the outer function exits

I hoped the closure would keep the values each time I stored a reference to the closure

For example:

function of()
{
var x = 1;

function inf() { alert(x); }

var ret = inf;

x = 2;

return ret;
}

var r = of();
r();

results in an alert box showing '2'

12) to delete a cookie, some programmers recommend you set the expiry date to the first second of 1st Jan 1970. (Unix timestamps start at 1970)

The idea is that setting the expiry date to a a few days ago might not delete the cookie if the user has set the time wrong

13) the 'window.onresize' event may not fire when you change the text size using the menus of the browser

I check every few seconds to see if the size of an element on the page has changed using 'setTimeout'.

I have read on the web that you can also use 'custom events' to do this

14) the 'onresize' event of the 'window' object fires more often than just when you resize the browser window

For example, the event fires when scrollbars appear when the page is too big

15) one way to have sound on Internet Explorer version 8 and earlier is to use the 'bgsound' tag.

When you give a value to the 'src' attribute of this tag the sound begins to play (asynchronously). Any sound currently playing stops

I store how long each sound takes within the game. This data is quite large. I create a small version of the game which does not have the lengths of the sound

16) I noticed part of the Dojo code uses 'new Array'. People seem to recommend using '[]'

The Dojo code occasionally uses the 'with' statement and puts 'var' declarations in 'for' loops (e.g. 'for (var i = 0; i < l; i++)' People recommend against using these too

17) the log files produced when my browser crashed almost filled up my disk!

You can find more tips in the wiki of the British Bingo project on Sourceforge

Posted by Bert Beckwith 2013-05-28