Category Archives: micro tutorial

JavaScript closures in brief

What’s a closure

Well every javascript programer knows that a variable defined in javascript file with the var declaration is global into all javascript code.

var a = 12;
console.log(a);

that code prints 12 into the console.

Note: console object is visible in Firefox and Safari, but not in IE, and here’s used just for the test!

Here comes the closures

Let’s assume I’ve two javascript files.

file1.js:

var a = 12;

file2.js:

a = 13;

Than obviously if I print out the variable a in file2.js after that line, I’ll have the value of a equal to 13, and this will be the value of the variable a from the file1.js, because that’s obviously the same variable. Continue reading JavaScript closures in brief →

jQuery OS detection

JavaScript libraries and OS

Particularly in that case I’m using jQuery which is enough famous, so I’m not going to introduce it, but however you can use any library, and this remains of your choice. The interesting part here is that I didn’t find a jQuery specific OS detecting plugin.

Reference to the roots: www.quirksmode.org

Well the guru PPK has the right answer, which is working with every JavaScript library … because it’s pure JavaScript itself. Continue reading jQuery OS detection →

jquery check for element existence in the DOM

Select the element

If you’re familiar with jQuery, which I suppose is true, you know how to select an element, for instance if there’s an element with id element_id, you do:

$('#element_id')

If the element exists in the DOM

if ( $('#element_id').length > 0 )
       console.log('the element with element_id
                    exists in the DOM');

Note: that console.log is present as console writing function under FF and Safari only.

if not …

Than $(‘#element_id’).length equals to 0

if ( $('#element_id').length == 0 )
       console.log('the element with element_id
                    don't exists in the DOM');

JavaScript disable mouse wheel

Attaching Event Listeners in JavaScript

Everybody knows how different is to attach event listeners under different browsers. Under IE is attachEvent(), while under FF is addEventListener()! The compatibility can be checked in the PPK site www.quirksmode.org where the author describes in detail where a given event listener can be registered and how it should be done.

Event Bubbling

Important part of the events in JavaScript is the so called “event bubbling“. You know, when you click on a given DOM element when bubbling is on the DOM elements containing this specific DOM element receive the event. So when I move the mouse over a DIV element the event mousemove is received in every element containing that DIV up to the window object.

In Mozilla it should look like this:

element.addEventListener('mousemove', callback, false);

Where the third parameter is describing the use of event bubbling, when setup to false, and if it’s set to true the other type of event handling is used, the so called capturing, which in breaf is the oposite of bubbling and the event is propagated from the top to bottom. The problem under different browsers is that sometimes this “mousemove” is not enough. In the case of mousewheel there are several combinations.

In IE:

document.attachEvent('onmousewheel', function(e){
     if (!e) var e = window.event;
     e.returnValue = false;
     e.cancelBubble = true;
     return false;
}, false);

In Safari:

document.addEventListener('mousewheel', function(e){
    e.stopPropagation();
    e.preventDefault();
    e.cancelBubble = false;
    return false;
}, false);

In Opera:

document.attachEvent('mousewheel', function(e){
    if (!e) var e = window.event;
    e.returnValue = false;
    e.cancelBubble = true;
    return false;
}, false);

In Firefox:

document.addEventListener('DOMMouseScroll', function(e){
    e.stopPropagation();
    e.preventDefault();
    e.cancelBubble = false;
    return false;
}, false);

Note the differences between all the browsers.