Thursday, March 24, 2011

Disable history back if pressing backspace

If you press backspace on the website page (but not on textbox) it will go to its previous page
This is to disable them

Taken from here, then I modified
http://www.sitepoint.com/forums/javascript-15/disable-back-javascript-168890.html

These is javascript coding
//DISABLE "GO BACK TO PREVIOUS HISTORY" IF PRESSING "BACKSPACE"
if (typeof window.event != 'undefined')
    document.onkeydown = function()
    {
        if( event.keyCode == 8){
            theEvent = event.srcElement.tagName.toUpperCase();
            if( theEvent == 'HTML'){
                return false;
            }
            else if( theEvent == 'INPUT'){
                theType = event.srcElement.getAttribute('type').toUpperCase();
                //alert(theType);
                switch(theType){
                    case 'TEXT':
                        return true;
                        break;

                   default:
                        return false;
                }
                return false;
            }
            else{
                switch( theEvent){
                    case 'TEXTAREA':
                        return true;
                        break;

                    default:
                        //alert('Backspace pressed on ' + theEvent + ', return false');
                        return false;
                }

                return false;
            }
        }
    }
else
    document.onkeypress = function(e)
    {
        if( e.keyCode == 8){
            theEvent = e.target.nodeName.toUpperCase();
            if( theEvent == 'HTML'){
                return false;
            }
            else if( theEvent == 'INPUT'){
                theType = e.target.getAttribute('type').toUpperCase();
                //alert(theType);
                switch(theType){
                    case 'TEXT':
                        return true;
                        break;

                   default:
                        return false;
                }
                return false;
            }
            else{
                switch( theEvent){
                    case 'TEXTAREA':
                        return true;
                        break;

                    default:
                        //alert('Backspace pressed on ' + theEvent + ', return false');
                        return false;
                }

                return false;
            }
        }
    }

No comments: