« l33t gray 2.0 - b2evolution skinMac Versus PC (the musical) »

Prevent scrolling in Firefox via key press

Technical, Javascript, 205 words   English (AU) Bookmark and Share

Firefox got me the other day with this one when I was trying to manually scroll the contents of an iframe because I was trying to achieve a bouncing effect when scrolling between different sections. And for some reason, Firefox would often mysteriously scroll me further then where I was suppose to go.

To best illustrate, have a look at this demo page which shows 2 iframes with slightly different content. In Safari and IE, using arrow up/down on both iframes examples will not scroll the text. However, in FF, if return false is not present in the event, then the scrolling will still occur.

...

The first iframe without return false has the following Javascript code,

Code:

<script type="text/javascript">
keyCatcher=function(e)
{
        if(e)
        {
                if (e.stopPropagation) e.stopPropagation();
                e.cancelBubble = true;
                e.returnValue = false;
        }
}
 
document.onkeypress=keyCatcher;
 
</script>

And the missing piece of the FF puzzle is in second iframe

Code:

<script type="text/javascript">
keyCatcher=function(e)
{
        if(e)
        {
                if (e.stopPropagation) e.stopPropagation();
                e.cancelBubble = true;
                e.returnValue = false;
        }
        /* You need to return false here for Firefox! */
        return false;
}
 
document.onkeypress=keyCatcher;
 
</script>

Has been tested and known to affect both FF 2 and FF 3

Trackback address for this post

This is a captcha-picture. It is used to prevent mass-access by robots.
Please enter the characters from the image above. (case insensitive)

No feedback yet

Leave a comment


Your email address will not be revealed on this site.
(Line breaks become <br />)
(For my next comment on this site)
(Allow users to contact me through a message form -- Your email will not be revealed!)
This is a captcha-picture. It is used to prevent mass-access by robots.
Please enter the characters from the image above. (case insensitive)
February 2012
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29        
Brinley Ang

Brinley Ang is a sysadmin, web dev, coder, geek boy, jedi knight fragger, caffine addict, deaf meloncholic and rockstar wannabe. Listens to the sex pistols and a wide assortment of heavy metal.

Make payments with PayPal - it's fast, free and secure!
Search
home | portfolio | blog | links | contact
© 2012 - Brinley Ang
Valid xhtml