使用者工具

網站工具


webdesign:ux

User Experience

Tools

Misc

防止誤觸Backspace鍵跳離編輯中網頁

http://blog.darkthread.net/post-2011-07-26-prevent-backspace-goback-in-browser.aspx

    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.2.js" 

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

     <script type="text/javascript">

         $(function () {

            //除了input text外,停用Backsapce,防止不慎回前一頁

             $(document).keydown(function (e) {

                 //採用demo建議 多考量"textarea"

                 if (e.which == 8 && (e.target.type == "text" || e.target.type == "textarea")) 

                        e.preventDefault();

             });

         });

     </script>
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.2.js" 

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

     <script type="text/javascript">

         $(function () {

             function setUnloadWarning(enabled) {

                 window.onbeforeunload =

                    enabled ? showWarning : null;

             }

             function showWarning() {

                 return "資料尚未儲存,確定要離開嗎?"

             }

             $("input:text").live("change", function () {

                 setUnloadWarning(true);

             });

             $("input:submit").click(function () {

                 setUnloadWarning(false);

             });

         });

     </script>
webdesign/ux.txt · 上一次變更: 2016/03/04 21:51 由 sars