Tuesday, February 8, 2011

OnMouseDown in Firefox blocking OnMouseMove. Fix: add "return false;" in OnMouseDown

In your "onmousedown" event handler of a DOM element in Firefox (FF), make sure you add a "return false;" in case you have problems. That could solve your problem.

This was the solution in my case. I had a problem because not returning false from that handler caused the element "to be dragged by FF": that is, after i mousedown-ed on it and then started to move without releasing the mouse, FF initiaited that "draggable behaviour", like it does for the images, where the the element follows your mouse pointer and is a bit transparent.

So, while still with the mouse down:
  • FF initiaited that "draggable behaviour"
  • this was stopping document.onmousemove handler to act (that i had assigned in the onmousedown handler).
Releasing the mouse pointer had another strange effect:
  • onmouseup was also NOT triggered and,
  • just after that, document.onmousemove was "free" and acting.
So i ended up with the mouse up and document.onmousemove acting - which is not what i wanted.

I was implementing a kind of scroller(see code snippet bellow) where you mousedown the cursor of the scroll, move you mouse around to make the cursor move and then release the mouse and cancel the cursor move.
This is a snippet of the code that works; note the "return false;"


/*snippet of code, for all code email me*/

/*when mousedown-ing the cursor of the scroll, prepare the "real-time"
update of the cursor and content position(thanks to document.onmousemove).
I used jquery to avoid the differences between browsers
*/

$(cursor).mousedown(function(event){
    registerReferences(event);
    $(document).mousemove(scroll);
    return false;
});


/*this does the actual move of the cursor and the content we scroll with this scroller
*/

function scroll(event){
    var newValue = cursorTop + event.screenY - y;
    if (newValue < min) 
        newValue = min;
    else 
        if (newValue > max) 
            newValue = max;
    cursor.style.top = appendUnit(newValue);
    setContentMarginTop(newValue);
}


/*relasing the mouse cancels modification of the position of the cursor and content
*/

$(document).mouseup(function(event){
    $(document).unbind('mousemove');
});
...

Before adding "return false;", on the first mouse down and mouse up the scrolling worked. The above problem started the second time i tried to manipulate the scroll. So this must be a consequence of the very first onmousedown call not having returned false. The third and the n-th attempt behaved like the 2nd: not working.

I found the "return false;" mention here googling with "firefox drag disable". I tried it in my case and that did it.

Sunday, December 19, 2010

Welcome to the blog of theapplicationbox.com website.

Hi, I'm Vlad BALAN, from theapplicationbox.com The future posts will be my way to log various experiences i had related to JEE, HTML/CSS, Javascript/Ajax and other Java and webdesign subjects: workarounds, solutions, answers, etc. that i found on web explained by others and that should be known by many of us, developers.

Use "hibernate.hbm2ddl.auto=update" to update your schema (add remove columns, etc.) without losing data in tables.

Use <property name="hibernate.hbm2ddl.auto" value="update" /> to update your schema (add remove columns, etc...