amit chauhan
amit chauhan

Reputation: 59

i want to refresh my page 5 times on one click

How can i refresh my page 5 times on just one click here is my code

<!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;
}
}

Upvotes: 1

Views: 575

Answers (1)

vittore
vittore

Reputation: 17589

You can add query string parameter refreshesLeft and on button click redirect to mypage?refreshesLeft=5. Add onLoad handler to check if you have query string parameter set, and if yes, decrement it and redirect.

<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>

Upvotes: 3

Related Questions