Tuesday, 28 January 2014

How to prevent backspace from navigating back to previous page using javascript or jquery



There will be cases when we may require to prevent backspace navigation in web pages.. This can be easily donw with javascript/jQuery using the following code. Just space this code in the scripts section of your code.


$(function () {
        var rx = /INPUT|TEXTAREA/i;
        $(document).bind("keydown keypress", function (e) {
            if (e.which == 8) { // 8 == backspace
                if (!rx.test(e.target.tagName) || e.target.disabled || e.target.readOnly) {
                    e.preventDefault();
                }
            }
        });


    })


No comments:

Post a Comment