JS Disable Browser Back button

Source: Internet
Author: User

When the keyboard knocks down the back key (Backspace)

1. Disable browser auto-fallback

2, but does not affect the password, single line text, multi-line text input box and so on fallback operation

//method One:////When the keyboard knocks down the back key (Backspace)//1. Disable browser auto-fallback//2, but does not affect the password, single line text, multi-line text input box and so on fallback operation//window.onload = function () {//    /**************************** //* [email protected] *//* Time: 2012-08-20 *//* version:2.1 *//     *                          * //     ****************************///document.getElementsByTagName ("body") [0].onkeydown = function () {//        //Get Event Object//var elem = Event.relatedtarget | | event.srcelement | | event.target | | event.currenttarget;//if (Event.keycode = = 8) {//determine the key to backspace//            //gets the element that the cursor is pointing to when the key is pressed//var elem = event.srcelement | | event.currenttarget;//            //determine if you need to prevent the default delivery of events that press the keyboard//var name = Elem.nodename;//if (name! = ' INPUT ' && name! = ' TEXTAREA ') {//return _stopit (event);//            }//var type_e = Elem.type.toUpperCase ();//if (name = = ' INPUT ' && (type_e! = ' TEXT ' && type_e! = ' TEXTAREA ' && type_e! = ' PA ssWOrd ' && type_e! = ' FILE ') {//return _stopit (event);//            }//if (name = = ' INPUT ' && (elem.readonly = = True | | elem.disabled = = true)) {//return _stopit (event);//            }//        }//    }//}//function _stopit (e) {//if (e.returnvalue) {//E.returnvalue = false;//    }//if (e.preventdefault) {//E.preventdefault ();//    }//return false;//}//Method Two:////When the keyboard knocks down the back key (Backspace)//1. Disable browser auto-fallback//2, but does not affect the password, single line text, multi-line text input box and so on fallback operation//handling keyboard events prevents back key (Backspace) passwords or single-line, multiline text boxes except//function Banbackspace (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//    //gets the event type as a criterion//var vreadonly = Obj.getattribute (' readonly ');//var venabled = Obj.getattribute (' enabled ');//    //handling Null value cases//vreadonly = (vreadonly = = null)? false:vreadonly;//venabled = (venabled = = null)? true:venabled;//    //when the BACKSPACE key is hit, the event source type is password or single line, multiline text,//    //and the ReadOnly property is True or the Enabled property is False, the BACKSPACE key fails//var Flag1 = (Ev.keycode = = = 8 && (t = = "password" | | t = "text" | | t = "TEXTAREA")//&& (vreadonly = = True | | venabled! = TRUE))? True:false;//    //when the BACKSPACE key is hit, the event source type is non-password or single-line, multiline text, the BACKSPACE key is invalidated//var Flag2 = (Ev.keycode = = 8 && t! = "password" && t! = "text" && t! = "textarea")//? true:false;//    //Judging//if (flag2) {//return false;//    }//if (Flag1) {//return false;//    }//}////Disable Back button action on Firefox, Opera//document.onkeypress = banbackspace;////disable Back key action on IE, Chrome//document.onkeydown = banbackspace;//Method Three:////When the keyboard knocks down the back key (Backspace)//1. Disable browser auto-fallback//2, but does not affect the password, single line text, multi-line text input box and so on fallback operation//handling keyboard events prevents back key (Backspace) passwords or single-line, multiline text boxes except//<script language= "JavaScript" >//Javascript:window.history.forward (1);//</script>
View Code

The best solution for method Two, method one under Chrome and IE through under Firefox does not pass.

The contents are all sourced elsewhere:

http://volunteer521.iteye.com/blog/830522/

Wait a minute.

JS Disable Browser Back button

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.