代码之家  ›  专栏  ›  技术社区  ›  Zac

如何在Javascript中使用Ajax使Android手机的后退按钮工作?

  •  0
  • Zac  · 技术社区  · 7 年前

    我有以下代码来处理Javascript框架7中设备的后退按钮。我想知道,如果所有后续页面都在使用Ajax,如何使用“后退”按钮返回到以前的页面。此时,按下后退按钮,如果用户想要退出应用程序,总是会提示用户。该应用程序有20个页面,但它们都显示为Ajax页面,而不是HTML页面。

    <!-- We don't need full layout here, because this page will be parsed with Ajax-->
    

    请帮忙。谢谢你的帮助!

            function onLoad() {
                document.addEventListener("deviceready", onDeviceReady, false);
            }
            // device APIs are available
            function onDeviceReady() {
                // Register the event listener
                document.addEventListener("backbutton", onBackKeyDown, false);
                document.addEventListener("menubutton", onMenuKeyDown, false);
            }
            // Handle the back button
            function onBackKeyDown(e) {
                e.preventDefault();
                navigator.notification.confirm("Are you sure you want to exit?", onConfirm, "Confirmation", "Yes,No");
                //navigator.app.backHistory();
    
                if (document.getElementById('#homepage')) {
                    e.preventDefault();
                    navigator.app.exitApp();
                    navigator.notification.confirm("Are you sure you want to exit?", onConfirm, "Confirmation", "Yes,No");
                } else {
                    navigator.app.backHistory();//This line doesn't work when tried on the if function - it seems useless - maybe not supported anymore?
                    //the code never gets here coz other pages are simply loaded as Ajax, and there is only 1 index.html - how to fix?
                }
            }
    
            function onConfirm(button) {
                if (button == 2) { //If the user selected No, then we just do nothing
                    return;
                } else {
                    navigator.app.exitApp(); // Otherwise we quit the app.
                }
            }
            // Handle the menu button
            function onMenuKeyDown() {}
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   Yusril Herlian Syah    7 年前

    在framework 7应用程序中,您可以使用 mainView.router.back() .

    function onBackKeyDown(e) {
    
        if(mainView.activePage.name == 'yourhomepagename') {
            e.preventDefault(); 
            navigator.notification.confirm("Are you sure you want to exit?", onConfirm, "Confirmation", "Yes,No");
        } else {
            mainView.router.back({ ignoreCache: true });
        }
    
    }
        2
  •  0
  •   dloeda    7 年前

    if (document.getElementById('#homepage')) {

    当您在另一个页面上时,您可以检查这个HTML元素是否具有类或特定样式。

    如果您使用的是ajax,我想这个HTML元素将有一个 hidden 类别或a display: none

    document.querySelectorAll('#content[style="display: none;"]') document.querySelectorAll('#content.hidden')