简体   繁体   English

后退按钮事件监听器

[英]Back button Event Listener

I have a Single page application. 我有一个单页应用程序。 In that at the first instance I am displaying the Input page and the Output Div is hidden at that point of time.Later on when the Input data are submitted I hide the Input data and Use ajax call to calculated the the output and display the output result in the Output div. 首先是显示Input页面,并在那个时间点隐藏Output Div。稍后提交Input数据时,我隐藏Input数据并使用ajax调用来计算输出并显示输出结果在输出div中。 So basically the same page is present both for input and output page. 因此,输入和输出页面基本上都存在相同的页面。 Now I have a back button, when users click on that the Input div is shown and Output div is hidden. 现在,我有一个后退按钮,当用户单击该按钮时,将显示Input div,而Output div隐藏。

$("#renderOutput").on("click", "#chngeIcon", function () {
    $('#renderOutput').hide();
    $('#InputPage').show();
});

Since it is a single page application if user click on the browser back button, than it is taken to previous visited site. 由于如果用户单击浏览器的后退按钮,它是一个单页应用程序,那么它将被带到以前访问的站点。 I want to make back button to behave similar to my back button. 我想使后退按钮的行为类似于我的后退按钮。 Please help me. 请帮我。 enter image description here 在此处输入图片说明

You can try popstate event handler, eg: 您可以尝试popstate事件处理程序,例如:

window.addEventListener('popstate', function(event) {
    // The popstate event is fired each time when the current history entry changes.



    if ($("#InputPage").is(":visible") == false) {
        // Call Back button programmatically 
        history.back();
        // Uncomment below line to redirect to the previous page instead.
        // window.location = document.referrer
    } else {
        // Stay on the current page.
        history.pushState(null, null, window.location.pathname);
        $('#renderOutput').show();
        $('#InputPage').hide();
    }

    history.pushState(null, null, window.location.pathname);

}, false);

Hope that helps 希望能有所帮助

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM