我如何一键刷新5次页面,这是我的代码

<!DOCTYPE html>
 <html>
  <head>
   <title>Page Title</title>
  </head>
<body>
   <button type="button" onClick="Refresh()">Close</button>

 <script>

function Refresh() {

for (var i=0; var i<5; var i++) {
    window.parent.location = window.parent.location.href;
}
}

最佳答案

您可以添加查询字符串参数refreshesLeft,并在按钮上单击重定向到mypage?refreshesLeft=5。添加onLoad处理程序以检查是否设置了查询字符串参数,如果是,则将其递减并重定向。

<button type="button" onClick="Refresh()">Close</button>

<script>

function Refresh(refreshesLeft) {
  refreshesLeft = refreshesLeft || 5;
  window.parent.location = window.parent.location.href + '?refreshesLeft='+refreshesLeft;
}

function onLoad() {
  let params = new URLSearchParams(document.location.search.substring(1));
  let rl = params.get("refreshesLeft")
  if (rl) Refresh( (rl | 0 ) -1)
}

document.addEventListener("DOMContentLoaded", onLoad)

</script>

09-15 12:50