我想一次点击我的页面5次

时间:2017-04-18 13:28:41

标签: javascript page-refresh

如何只需点击一下即可刷新我的页面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;
}
}

1 个答案:

答案 0 :(得分:3)

您可以添加查询字符串参数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>