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

时间:2018-03-29 11:46:55

标签: javascript jquery

如果在iOS和&中按下后退按钮,此代码将转发机器人:

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

此代码在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>

我们也可以制作适用于iOS和iOS的代码。使用preventBack()函数回击或向后滑动时刷新android。有可能吗?

1 个答案:

答案 0 :(得分:0)

使用以下代码,这将有助于您停止点击并重新加载页面。

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