Alexandra
Alexandra

Reputation: 27

Disable Backspace

I want to disable the Backspace button any time I click the browser page. I have written this piece of code (the second if is making sure that this would work for any version of IE - 11 or lower):

<PUBLIC:COMPONENT TAGNAME="menucontrol">
    <PUBLIC:ATTACH EVENT="onclick" FOR="document" ONEVENT="RemoveBackspace();"/>

<SCRIPT language="javascript">

function RemoveBackspace() {
           document.onkeydown = function (){

            if(event.keyCode === 8) {
                if(typeof event.preventDefault === 'function'){
                   event.preventDefault();
            }
            else{
                   event.returnValue = false;
            }
        }
};
}
</SCRIPT>
...
</PUBLIC:COMPONENT>

If I introduce an alert function in the RemoveBackspace() function, the message appears. I don't know what's wrong with this code. Should I use a different approach?

Upvotes: 0

Views: 102

Answers (1)

yesman
yesman

Reputation: 7829

You forgot to pass the event to the function.

document.onkeydown = function (event){

        if(event.keyCode === 8) {
            event.preventDefault();
        }
};

Look at the first line of my code. I also removed the unnecessary second if/else statement, as the code works fine without it. Don't worry, everyone makes mistakes like this at first. :P

Upvotes: 1

Related Questions