locked
Read Only Textbox - Backspace Key Causes Page Back RRS feed

  • Question

  • User-1165521811 posted

    Hi all,

    I'm looking for a simple solution to disable the IE functionality of the backspace key behaving like a back button. I have textboxes that are dynamically set to read only depending on certain criteria. If the user hits the backspace key when focus is on a read only textbox it navigates back a page. I would like to disable this functionality.

    Please advise.

    Thanks!

    Friday, April 23, 2010 11:30 AM

Answers

  • User2090264947 posted

    Try this javascript


    <script type="text/javascript">
    function killBackSpace(e){
    e = e? e : window.event;
    var t = e.target? e.target : e.srcElement? e.srcElement : null;
    if(t && t.tagName && (t.type && /(password)|(text)|(file)/.test(t.type.toLowerCase())) || t.tagName.toLowerCase() == 'textarea')
    return true;
    var k = e.keyCode? e.keyCode : e.which? e.which : null;
    if (k == 8){
    if (e.preventDefault)
    e.preventDefault();
    return false;
    };
    return true;
    };
    if(typeof document.addEventListener!='undefined')
    document.addEventListener('keydown', killBackSpace, false);
    else if(typeof document.attachEvent!='undefined')
    document.attachEvent('onkeydown', killBackSpace);
    else{
    if(document.onkeydown!=null){
    var oldOnkeydown=document.onkeydown;
    document.onkeydown=function(e){
    oldOnkeydown(e);
    killBackSpace(e);
    };}
    else
    document.onkeydown=killBackSpace;
    }
    </script>




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 23, 2010 1:57 PM
  • User551462331 posted

    just to stop browser back on readonly textbox, disabling back button feature on u r page entirely is not good idea.

    first of all, readonly property of textbox signifies that, there's no focus on the editable control(even if u can see cursor blinking. but it cant take the input)

    and in case, no editable control having focus on web page, all keys start to work for browser command. so, pressing back button causes browser back.

    to avoid that, u can set onFocus property of readonly textbox to some javascript function in which focus can be shifted to next editable textbox.

    like this

    onFocus="MoveFocus()"

    function MoveFocus()

    {

       document.getElementById("txtEditable").focus();

    }

    so, as soon as user click on readonly textbox cursor will move to another textbox which is editable (this also make sense. as, readonly textbox can not use focus) and back button click will cause text clear and not browser back

    hope this helps...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 23, 2010 2:39 PM

All replies

  • User533502567 posted

    I'm looking for a simple solution to disable the IE functionality of the backspace key behaving like a back button.
     

    May be this helps you..

    http://www.vijaykodali.com/Blog/post/2008/09/02/Disable-Backspace-to-Previous-Page-in-AspNet.aspx

    Friday, April 23, 2010 11:35 AM
  • User-1165521811 posted

    Thanks for the reply.

    That code does not seem to work...the backspace key still navigates back a page when pressed inside a read only textbox.

    Any other ideas?  

    Friday, April 23, 2010 12:29 PM
  • User533502567 posted

    the backspace key still navigates back a page when pressed inside a read only textbox.
     

    How can you press a key in read only textbox? If you try to put focus on read only textbox, focus will shift to next or previous control.

    Friday, April 23, 2010 1:40 PM
  • User-216961763 posted

    <script language="javascript" type="text/javascript">

            function StopBack()

            {

                window.history.forward(1);

            }       

        </script>

     

    -- Please write the above code in Header Tab 

    and Below code in Your <Body> Tag

    onload="StopBack();"

    This is u need to write in the page where u want to disable Back button 

    Friday, April 23, 2010 1:46 PM
  • User2090264947 posted

    Try this javascript


    <script type="text/javascript">
    function killBackSpace(e){
    e = e? e : window.event;
    var t = e.target? e.target : e.srcElement? e.srcElement : null;
    if(t && t.tagName && (t.type && /(password)|(text)|(file)/.test(t.type.toLowerCase())) || t.tagName.toLowerCase() == 'textarea')
    return true;
    var k = e.keyCode? e.keyCode : e.which? e.which : null;
    if (k == 8){
    if (e.preventDefault)
    e.preventDefault();
    return false;
    };
    return true;
    };
    if(typeof document.addEventListener!='undefined')
    document.addEventListener('keydown', killBackSpace, false);
    else if(typeof document.attachEvent!='undefined')
    document.attachEvent('onkeydown', killBackSpace);
    else{
    if(document.onkeydown!=null){
    var oldOnkeydown=document.onkeydown;
    document.onkeydown=function(e){
    oldOnkeydown(e);
    killBackSpace(e);
    };}
    else
    document.onkeydown=killBackSpace;
    }
    </script>




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 23, 2010 1:57 PM
  • User551462331 posted

    just to stop browser back on readonly textbox, disabling back button feature on u r page entirely is not good idea.

    first of all, readonly property of textbox signifies that, there's no focus on the editable control(even if u can see cursor blinking. but it cant take the input)

    and in case, no editable control having focus on web page, all keys start to work for browser command. so, pressing back button causes browser back.

    to avoid that, u can set onFocus property of readonly textbox to some javascript function in which focus can be shifted to next editable textbox.

    like this

    onFocus="MoveFocus()"

    function MoveFocus()

    {

       document.getElementById("txtEditable").focus();

    }

    so, as soon as user click on readonly textbox cursor will move to another textbox which is editable (this also make sense. as, readonly textbox can not use focus) and back button click will cause text clear and not browser back

    hope this helps...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 23, 2010 2:39 PM