Friday, April 13, 2012

JavaScript - Disabling the TAB key on page

Yep, sometimes you want to disable the TAB key on your web page, for example if you want to eliminate that orange frame there is when the user "selects" images or links.
That's the way it works:

    <%--disabling the "Tab" key on page--%>
    <script type="text/javascript">               
        function cancelKey(e) {
            var e = (window.event) ? event : e;
            if (e.keyCode == 9) { // Tab
                if (window.event) {
                    window.event.returnValue = false;
                    }
                else {
                    if (e.cancelable) { e.preventDefault(); }
                        }
                    }
                }
                document.onkeydown = cancelKey;
    </script>



Cheers!

1 comment: