Giter Site home page Giter Site logo

backspace-disabler's Introduction

BACKSPACE DISABLER

Disable the annoying backward navigation that occurs when the user press the backspace key.

Features

  • Prevent browser back on backspace (by using preventDefaut())
  • Does not break ability of user to delete content on all known input types
  • Supports contentEditable (in read/edit mode)
  • Allow to apply the behavior globally or on a subtree
  • No dependency

Installation

NPM:

npm install backspace-disabler

Or just copy the code inside index.js into your codebase.

Usage

Require it with CommonJS loader (Browserify / Webpack...)

var BackspaceDisabler = require("backspace-disabler");

// Disable the backspace that triggers backward navigation
BackspaceDisabler.disable(); 
// Revert to normal
BackspaceDisabler.enable();

// Can also be applied to a specific element tree:
var myWidget = document.getElementById("myWidget");
BackspaceDisabler.disable(myWidget);
BackspaceDisabler.enable(myWidget);

Credits

The ideas of this lib come from StackOverflow

Hire a freelance expert

Looking for a React/ReactNative freelance expert with more than 5 years production experience? Contact me from my website or with Twitter.

backspace-disabler's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

backspace-disabler's Issues

ConnectedToTheDOM Regression

I have a bit of a strange use case that no longer works after #4 went in.

In my application, I have a parent window that spawns secondary windows. The parent window then disables these secondary windows by passing the secondary window references to BackspaceDisabler.disable(secondaryWindow). The issue is that when I hit backspace in a form element (or content-editable) in the secondary window, the event is prevented. This is because the document that is found by traversing node.parentNode in connectedToTheDom (the secondary window's document) is a different reference than the global document (the primary window's document).

One potential fix is to check the nodeType of the node instead of checking the reference against the document global. If node.nodeType === Node.DOCUMENT_NODE, we've found the closest parent document.

If this seems acceptable then I can submit a pull request for it.

cc @fresheneesz

npm module?

Would be nice to be able to pull this in with npm

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.