React v16.5.2 - Raising a popup message with an unconditional screen refresh afterwards

146 Views Asked by At

I have a requirement to display a popup message (similiarly to the outcome of the "confirm" function of Alertify.js), and to unconditionally refresh the screen after the popup is being closed by the user, whether it is done through the 'X' or the 'OK' button.

Basically, the "confirm" function proposed by Alertify.js adheres to my need except its "Cancel" button (as it does nothing after the user clicks on it). If I could get rid of the cancel button somehow that would be great, but I'm not sure how to or if I'm even able to at all. What work-around would you suggest to that matter?

I tried this code,

alert("Successful Message");
location.reload();

This code causes the screen to refresh but doesn't show the alert. I guess the second line doesn't wait for the alert message to be closed and just immediately reloads the screen.

Thanks in advance!

1

There are 1 best solutions below

0
Renaldo Balaj On

Here you have a working demo :)

[i see that you are new so click on the BLUE BUTTON 'Run code snippet', and comment if it worked ]

function myFunction() {
  var txt;
  if (confirm("Press a button!")) {
    txt = "You pressed OK!";
  } else {
    txt = "You pressed Cancel!";
  }
  document.getElementById("demo").innerHTML = txt;
}
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Confirm Box</h2>


<button onclick="myFunction()">Try it</button>

<p id="demo"></p>


</body>
</html>