简体   繁体   English

按下后退按钮时如何使用preventBack()刷新页面?

[英]How to use preventBack() to refresh a page when pressing back button?

This code forwards if pressing back button in both iOS & Android: 如果同时在iOS和Android中按下返回按钮,则此代码将转发:

    <script type = "text/javascript" >
    function preventBack(){window.history.forward();}
    setTimeout("preventBack()", 0);
    window.onunload=function(){null};
    </script>

This code refreshes a page when hitting or swiping back in iOS but not working in android: 在iOS中点击或向后滑动时,此代码刷新页面,但在android中不起作用:

    <input type="hidden" id="refreshed" value="no">
    <script type="text/javascript">
    onload=function(){
    var e=document.getElementById("refreshed");
    if(e.value=="no")e.value="yes";
    else{e.value="no";location.reload();}
    }
    </script>

So can we make a code that works in both iOS & android to refresh when hitting back or swiping back using preventBack() function. 因此,我们可以制作一个可以在iOS和android中使用的代码,以使用preventBack()函数回击或向后滑动时刷新的代码。 Is it possible? 可能吗?

Use the below code, this will helps you to stop click back and reload the page too. 使用下面的代码,这将帮助您停止单击并重新加载页面。

function preventBack() {
  history.pushState(null, null, document.URL);
  window.addEventListener('popstate', function () {
    history.pushState(null, null, document.URL);
    location.reload();
  });
}

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

相关问题 按下后如何使用preventBack()删除div? - How to use preventBack() to remove a div when pressing back? 如何在MAC野生动物园中按返回按钮时强制页面刷新 - How to force a page refresh while pressing the back button in MAC safari 浏览器希望用户在按第二页上的后退按钮时重新提交/刷新第一页 - Browser wants the user to re-submit / refresh 1st page when pressing the back button on 2nd page 刷新页面时如何不显示返回页首按钮? - How to not show back-to-top button when refresh the page? 按浏览器后退按钮但不工作时尝试重定向到某个页面 - trying to redirect to some page when pressing browser back button but not working 如何刷新后退按钮点击页面? - How to refresh page on back button click? 通过按后退按钮进行访问时自动重新加载页面 - Automatic page reload when accessed from pressing the back button 反应本机应用程序,按android-phone后退按钮应该刷新上一个页面或组件? - React native app, pressing android-phone back-button should refresh previous page or component? 从后退按钮访问页面时强制页面刷新 - Force a page refresh when page visited from back button 当后退按钮触发popState时,如何防止页面刷新? - When the back button triggers popState how can I prevent a page refresh?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM