Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7950901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:24:29+00:00 2026-06-04T02:24:29+00:00

I’m using this to disable the ‘scrolling’ effect the spacebar has in a browser.

  • 0

I’m using this to disable the ‘scrolling’ effect the spacebar has in a browser. Will this affect other keypress events too?

window.onkeydown = function(e) {
    return !(e.keyCode == 32);
};

Could someone please explain what this is doing? I’m not sure if this code is bad, but it seems to disable other keypress related codes in my page, and I want to make sure this isn’t the reason.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T02:24:32+00:00Added an answer on June 4, 2026 at 2:24 am

    ASCII code 32 is the ASCII value that represents the spacebar key, and your code is essentially telling the browser to return false whenever that keycode is detected. Since false is returned, the scrollbar effect you speak of is in fact successfully disabled.

    However, the unfortunate side effect of this convenient spacebar-scroll-disabling function is that it disables spacebar keypresses everywhere on the page.

    Instead of returning false, if the keycode is detected, pass the current scrollTop value into a closure that returns a function to a setTimeout event. When the setTimeout fires, the scrollTop position is reset back to the value it was in when the setTimeout event was first registered.

    window.onkeydown = function(e) {
        if(event.keyCode == 32) { // alert($(document).scrollTop() );
            setTimeout(                 
                (function(scrollval) { 
                    return function() { 
                        $(document).scrollTop(scrollval);
                    };
                })( $(document).scrollTop() ), 0);
        }
    };
    

    Your users can still conveniently make use of spacebars in input textboxes and textareas, and at the same time, pressing the spacebar key while not focused on a text element will no longer result in the page scrolling.

    Under the hood, the scroll is still taking place. It’s just being reset at a rate fast enough to where the user doesn’t notice.

    If you increase this value to 100 or 1000, it will give you a better idea of what is going on under the hood. You’ll actually see the page scroll and then get set back to the previous scroll position.

    This was only tested in Chrome and Firefox 13! So you may have to adjust the setTimeout duration — currently 0 — to a different value in browsers like Internet Explorer. Be prepared to gracefully degrade — by supporting this feature only in modern browsers — if necessary.

    UPDATE:

    For reference, below is the method to use to make this compatible in the major browsers. It has been tested in Chrome, Firefox, IE8, IE9, and Safari.

    While it does work in IE8/IE9, it isn’t very smooth.

    // put the eventhandler in a named function so it can be easily assigned
       // to other events.
    function noScrollEvent(e) {
        e = e || window.event;
        if(e.keyCode == 32) {  
            setTimeout(                 
                (function(scrollval) { 
                    return function() { 
                        $(document).scrollTop(scrollval);
                    };
                })( $(document).scrollTop() ), 0);
        }
    }
    
    
    // Chrome and Firefox must use onkeydown
    window.onkeydown = noScrollEvent;
    
    // Internet Explorer 8 and 9 and Safari must use onkeypress
    window.document.onkeypress = noScrollEvent;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.