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 7874845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:52:52+00:00 2026-06-03T02:52:52+00:00

I am making a page in which a user must be able to type.

  • 0

I am making a page in which a user must be able to type. The default function of backspace is to go back a page, however I need to prevent this and assign my own function to it.

The problem is actually preventing backspace. I can capture it however I can not prevent it.
I am using Level 3 event listeners. event.preventDefault() did not work for me and neither did return false.

I have tried this also:

function onunload(e) {
    if (e.keyCode == 37) return false;
    return true;
}

and

<body onunload="return false;">

However the first basically does

return confirm("false");

and the second does nothing?

  • 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-03T02:52:53+00:00Added an answer on June 3, 2026 at 2:52 am

    Preventing backspace key altogether might not be a good idea, assume you make a typo and want to fix it in a text box! So you need to disable backspace only if someone is not typing in a text input!

    This might work in all major browsers:

    document.onkeydown = function (e) {
        e = e || window.event;
        if (e.keyCode == 8) {                                           // Disable Backspace
            var el = document.activeElement;
            if (!((el.nodeName == "INPUT" && (el.type == "text" || el.type == "password")) || el.nodeName == "TEXTAREA")) {
                e.preventDefault();
                e.stopPropagation();
                return false;
            }
        }
    };
    

    DOM3 event model spec comes with backward compatibility so I don’t think this might be an issue, however this is the DOM3 modified version of the above code:

    function KeyDownHandler(e) {
        if (e.keyCode == 8) {                                           // Disable Backspace
            var el = document.activeElement;
            if (!((el.nodeName == "INPUT" && (el.type == "text" || el.type == "password")) || el.nodeName == "TEXTAREA")) {
                e.preventDefault();
                e.stopPropagation();
            }
        }
    };
    
    if (document.attachEvent) document.attachEvent("onkeydown", KeyDownHandler);
    else document.addEventListener("keydown", KeyDownHandler);
    

    A DOM3 event registration like the one above might be a great pain, John Resig has a good implementation on this, you could however use a standard cross browser library like JQuery which works fine in all major browsers!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a profile page in which I'm allowing the user to drag certain
I am making a web page which create user and a page from which
I am making a page which pulls from the user's browser their preferred language,
I am making a page in which there will be array of images. I
I am making a game and I have an options page which turns the
I'm making a page where the user upload a file. I want an if
Please have a look at this page www.pixeli.ca/issue. I have begun making a page
Unsure how to get this string to work correctly. Keeps making my page blank.
Im making a 3 page mobile optimised website. I need horizontal slide navigation as
I'm making an HTML5 tech demo for work in which a user can drag

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.