Web Development

Archive for the ‘javascript’ Category

One fact that many JavaScript developers don’t realize is that there are only two levels of scope in JavaScript – the global scope and the function scope. Usually we find code like:

function doSomething(){
    a = 1;
}

Here we are not defining a variable called ‘a’ we are just assigning a value to a global variable with the same name. Such statements inside function scope should be avoided for simple reason that global variables result in complicated data flow. The right way to do is:

function doSomething(){
    var a = 1;
}

The point is that although JavaScript is not strict about variable declaration, ‘a=1’ is not a convenient alternative for ‘var a=1’.

We have a layer, say an unordered list with links in it. We want to hide/show the layer. We can hide the layer by attaching a handler to mouse out event of the layer. However this doesn’t work as expected. Because of event bubbling and the way browsers implement this event, mouse out event is fired whenever mouse moves from the layer to a link within it (even though the mouse pointer is still inside the layer). This might cause flickering of the layer based on the implementation. One way out of this issue is to check on mouse out if the ‘related target’ element is the child of layer. If yes, do nothing else hide the layer. Related target is the element to which the mouse moves. None of the links within the ‘ul’ will qualify for the mouse out event handler but when the focus moves out of ‘ul’ to its parent the handler is invoked.

quirksmode has a detailed article on mouse events and the intricacies of the above mentioned issue.