简体   繁体   English

如何在单页应用程序的某些页面中禁用后退按钮

[英]How to disable the back button in certain pages of Single Page Application

My Application has page that displays the details in a list view. 我的应用程序具有在列表视图中显示详细信息的页面。 On clicking on the list item it will drill down to the page which shows its respective details. 单击列表项后,它将向下钻取至显示其各自详细信息的页面。

I need to navigate from the details page to the list page using the default back button in mobile or browser, but the same back button should not work when I click on it in the list view page. 我需要使用移动设备或浏览器中的默认后退按钮从详细信息页面导航到列表页面,但是当我在列表视图页面中单击它时,相同的后退按钮不起作用。

I tired to prevent the default action of the back button by adding the below code in the JS of that list page. 我厌倦了通过在该列表页面的JS中添加以下代码来防止后退按钮的默认操作。 However the back button is disabled for the whole application. 但是,整个应用程序都禁用了后退按钮。

document.addEventListener("backbutton", onBackKeyDown, false);

function onBackKeyDown(e) {
  e.preventDefault();
  // alert('Back Button is Pressed!');
}

Could anyone help me to solve this issue. 谁能帮我解决这个问题。 Thanks in Advance 提前致谢

To handle the back button behavior (simulating disable) you have to handle the navigation and a variable to know when navigation is permitted. 要处理后退按钮行为(模拟禁用),您必须处理导航和一个变量,以知道何时允许导航。

I made a page to show how, it works with browsers back button and Android back button. 我做了一个页面来展示如何与浏览器的后退按钮和Android后退按钮一起使用。

The variable disableBackButtonInThisPage is initially true, so navigation is not permitted. 变量disableBackButtonInThisPage最初为true,因此不允许导航。

A "Toggle disable" button changes the disableBackButtonInThisPage value, so when it is false, navigation is permitted. “切换禁用”按钮更改disableBackButtonInThisPage值,因此,如果为false,则允许导航。

<!DOCTYPE html>
<html lang="en">

<head>
    <script>
        history.pushState(null, null, location.href);
        var disableBackButtonInThisPage = true;
        window.onpopstate = function () {
            if (disableBackButtonInThisPage) {
                history.go(1);
            }else{
                history.go(-1);
            }
        };
        function toggleDisable() {
            disableBackButtonInThisPage = !disableBackButtonInThisPage;
            alert("Disable back button is: " + disableBackButtonInThisPage);
        }
    </script>
</head>

<body>
    <h1>Back button demo</h1>
    <p>Click browser back button or tap Android back button.</p>
    <button onclick="toggleDisable();">Toggle disable</button>
</body>

</html>

您必须使用某个变量来管理您所在的部分,并在每次更改时对其进行更新,在这种情况下,我将使用“ pagenow”

<button data-bind="enable: pagenow()!='listpage'">Back</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 禁用一页应用程序的浏览器后退按钮 - Disable browser back button for one page application 如何禁用某些页面的UpdateProgress(在主页上)? - How to disable UpdateProgress(on master page) for certain Pages? Firefox在单页应用程序上的后退按钮行为 - Firefox back button behaviour on single page application 单页应用程序和后退按钮[AJAX] - Single Page application and the back button[AJAX] 如果返回到 react-navigation 应用程序主页,如何禁用 Android 后退按钮? - How to disable Android Back Button if it brings back to the react-navigation application home page? 如何知道单页应用程序中的捕获后退按钮或前进按钮事件 - How to know catch Back Button or Forward Button events in single page application 在angularjs应用程序中注销后,如何通过浏览器后退按钮禁止用户访问上一页? - How to disable users from accessing previous page by browser back button after logout in angularjs application? 使用后退按钮移动单页应用程序AngularJS - Using back button Mobile Single Page application AngularJS 如何在AngularJS中导致相同的单页面应用程序时,如何显示后退按钮? - How to show the back button only if it leads to the same single-page-application in AngularJS? 仅在证书页面上禁用android后退按钮 - disable android back button only on certian pages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM