How to prevent the front-end javascript from pressing the backspace key to go back but not prevent the text box from using the backspace key to delete text

How to prevent the front-end javascript from pressing the backspace key to go back but not prevent the text box from using the backspace key to delete text

This code can:

document.onkeydown = function (e) {
    e.stopPropagation();//Prevent event bubbling delivery
    e.preventDefault();//Prevent the occurrence of browser default events
   //your code
    if (e.keyCode == 8) {//keyCode == 8 represents the back button pressed
         
    }
}

Let's correct it. The above writing has a serious problem:

Although this writing method shields the function of the return key page, but also, if the page has a text input box, then this input box cannot use the backspace key to delete text;

The following is an online search code that can block the page backspace key back, but also does not block the backspace key delete function. Thank you for sharing on the Internet, because there are several blogs that can find the same code, so it’s not possible. Determine who is the original:

<script type="text/javascript">
       //Handle keyboard events except Backspace password or single-line or multi-line text box
        function forbidBackSpace(e) {
            var ev = e || window.event;//Get event object 
            var obj = ev.target || ev.srcElement;//Get event source 
            var t = obj.type || obj.getAttribute('type');//Get event source type 
           //Get the event type as the judgment condition 
            var vReadOnly = obj.readOnly;
            var vDisabled = obj.disabled;
           //Handle the undefined value situation 
            vReadOnly = (vReadOnly == undefined)? false: vReadOnly;
            vDisabled = (vDisabled == undefined)? true: vDisabled;
           //When the Backspace key is pressed, the event source type is password or single-line or multi-line text, 
           //And the readOnly attribute is true or the disabled attribute is true, the backspace key is invalid 
            var flag1 = ev.keyCode == 8 && (t == "password" || t == "text" || t == "textarea") && (vReadOnly == true || vDisabled == true);
           //When the Backspace key is pressed, if the event source type is not a password or single-line or multi-line text, the backspace key is invalid 
            var flag2 = ev.keyCode == 8 && !(t == "password" && t == "text" && t == "textarea");
           //judgment 
            if (flag2 || flag1) return false;
        }
       //Prohibit the back key from acting on Firefox, Opera
        document.onkeypress = forbidBackSpace;
       //Prohibit the back key to act on IE, Chrome
        document.onkeydown = forbidBackSpace;
</script>
Reference: https://cloud.tencent.com/developer/article/1475576 ​​How does front-end javascript prevent the backspace key from being pressed and the page goes back without preventing the text box from using the backspace key to delete text-Cloud + Community-Tencent Cloud