|
|||||||
| JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...) |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
How To Disable Scrolling with the Arrow Keys in Firefox
Hi,
I am creating a Javascript web-app that will (hopefully) use the arrow keys to control some of the actions. The problem I'm running into is that when the up or down arrow keys are pressed, the page scrolls. I tried using the code below to stop the scrolling... function cancelEvent(e) { e = e ? e : window.event; if(e.stopPropagation) e.stopPropagation(); if(e.preventDefault) e.preventDefault(); e.cancelBubble = true; e.cancel = true; e.returnValue = false; return false; } but it didn't work. Any ideas? Suggestions? I appreciate your time and help. |
|
#2
|
||||
|
||||
|
Web pages can only use key combinations that the browser passes to them. The arrow keys have meaning to the browser and are therefore never passed to a web page.
__________________
Stephen Free Computer Help, blog, forum Web design ebooks and software JavaScript scripts and tutorials |
|
#3
|
|||
|
|||
|
That's odd... because in Firefox 3, the arrow keys being pressed triggers an event, and that event has a keyCode for the arrow keys. So it would seem the arrow keys do get passed to the browser when they're pressed... but is there any way to stop them from scrolling the page?
|
|
#4
|
||||
|
||||
|
The browser gets them before the page and so processes them first. Interfering with the normal functioning of someone's browser is not a good idea.
__________________
Stephen Free Computer Help, blog, forum Web design ebooks and software JavaScript scripts and tutorials |
|
#5
|
|||
|
|||
|
Ah gotcha. And I hear ya on the part about not interfering with standard browser functionality.
|
|
#6
|
||||
|
||||
|
[quote=felgall;940290]Web pages can only use key combinations that the browser passes to them. The arrow keys have meaning to the browser and are therefore never passed to a web page.[quote]
actualy I want you to test that theory: HTML Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>demo</title> <script type="text/javascript"> // <![CDATA[ window.onload = function(){ var temp = document.createElement('p'); temp.appendChild(document.createTextNode('lorem...')); for(var i = 0; i < 100; i++){ document.body.appendChild( temp.cloneNode(true) ); } addEvent(document.body, 'keydown', keyDown); addEvent(window, 'keydown', keyDown); } function addEvent(obj, evType, fn){ if(obj.addEventListener){ obj.addEventListener(evType, fn, false); return true; }else if(obj.attachEvent){ var r = obj.attachEvent("on"+evType, fn); return r; } } function keyDown(e){ var ev = e||event; var key = ev.which||ev.keyCode; var esc = 0; switch(key){ case 37: // left case 38: // up case 39: // right case 40: // down esc = 1; break; } if(esc && ev.preventDefault){ ev.preventDefault(); } return esc; } // ]]> </script> </head> <body> :p </body> </html>
__________________
If you are using PHP please use the [php] and [/php] forum tags for highlighting... The same applies to HTML and the forums [html][/html] tags. |
|
#7
|
|||
|
|||
|
hi scragar! great solution! have u also some for IE????
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|