control forward and back from browser

Asked

Viewed 545 times

0

I need to make the system not allow moving forward or backward when pressed to Backspace, someone can help me please?

Look forward to!

  • 1

    The user can always return the page by the "Back" button of the browser. When this happens the event window.onbeforeunload is fired but you cannot stop the action. More information here.

  • I need to know what to do, but I can’t remember how, I used once in a system that I no longer have in my hands today, but I know it has how to do, thanks for the help!

  • If you want to avoid browsing between pages of your system, do a SPA. Already if you want to remove a fundamental freedom from the user, take a vacation and use the time to rethink your life.

1 answer

-1

You guys, I figured it out:

$(document).unbind('keydown').bind('keydown', function(event) {
    var doPrevent = false;
    if (event.keyCode === 8) {
        var d = event.srcElement || event.target;
        if ((d.tagName.toUpperCase() === 'INPUT' && (d.type.toUpperCase() === 'TEXT' || d.type.toUpperCase() === 'PASSWORD' || d.type.toUpperCase() === 'EMAIL' || d.type.toUpperCase() === 'SEARCH' || d.type.toUpperCase() === 'FILE')) || d.tagName.toUpperCase() === 'TEXTAREA') {
            doPrevent = d.readOnly || d.disabled;
        } else {
            doPrevent = true;
        }
    }
    if (doPrevent) {
        event.preventDefault();
    }
});
  • when it is not inside some field it blocks the Backspace, preventing as soon as the system goes back to the previous page, because if the user has effected a change in some field he will lose this way, it helps, because if he clicks back there is another five hundred haha

  • 4

    tested on how many browsers?

  • 3

    only that the answer is only valid in the back (back) the forward does not resolve ( also does not solve if the user uses alt+left arrow )

  • 1

    Tested with the "Back" button of the browser?

  • 2

    @Nevershowmyface I believe his comment already answers this :), what he wants is to avoid the user coming back unintentionally when correcting a text field. This is one of several evils of using "web environment" as if it were real application.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.