Lord Jiine
Lord Jiine

Reputation: 1

How do I use button to refresh the browser page?

I have been learning JavaScript, and have been working on a customizable random number generator. If you decide you want to do more than one, it has to be refreshed. I have tried looping, but that makes it infinite. Here is the code:

<DOCTYPE! html>
<body>
  <script>
    //This is a simple thingy, to randomly pick a number under a certain number

    //This is how we get what they want the max to be
    var input = prompt("Pick a number to be the maximum");

    //This is what it will output
    var output = Math.floor(Math.random() * (input + 1));
    //The reason for +1 is that it never reaches the max number without it

    //This is the output
    alert(output);
  </script>
</body>

Upvotes: 0

Views: 528

Answers (5)

Travis Pettry
Travis Pettry

Reputation: 1352

If you would like to use a loop you can look at the following jsfiddle: http://jsfiddle.net/z1cv7s93/2/

I use a confirm to ask if the user wants to continue. If they do they will pick okay, else they chooses cancel and the loop exits.

Here is the code found in the fiddle:

var input; 
var contuine = true;
var output; 

while(contuine) {
    input = prompt("Pick a number to be the maximum");
    output = Math.floor(Math.random() * (input + 1)) 
    alert(output);
    contuine = confirm("Contuine?");
}

Upvotes: 1

Brad Christie
Brad Christie

Reputation: 101614

Some simple refactoring can help, but to answer the original question window.location.reload() is how your refresh a page.

With that said, you can make this entire block of code a function that can be called multiple times. As iut stands, this is executed once because it's not setup to be reusable. However, wrap it in a function:

function randomNumber(){
  */ our code */
}

Then call it on page load (can be right after the function declaration):

randomNumber()

Then (optionally) you can bind it to a button so it can be called again. e.g.

<button onclick="randomNumber();">Re-run</button>

Upvotes: 0

Esra
Esra

Reputation: 1180

You could do:

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Reload page</button>

<script>
function myFunction() {
    location.reload();
}
</script>

</body>
</html> 

W3Schools

Upvotes: 2

Samuel Goodell
Samuel Goodell

Reputation: 630

You can use JavaScript to refresh the page by setting the window.location property. To refresh, just set it to itself:

window.location = window.location;

To make it happen when a button is clicked, make a button in HTML:

<button id="refresh"></button>

Then in JavaScript using JQuery, wait until the page is loaded and then make it functional:

$(document).ready(function() {
    $("#refresh").click(function(event) {
        window.location = window.location;
    });
});

Upvotes: 0

Mwr247
Mwr247

Reputation: 1390

Assuming you wish to rerun the script vs actually reloading the page, you could do:

<button onlick="randomizer()">Generate Random</button>
<script>
  function randomizer(){
    var input = prompt("Pick a number to be the maximum");
    var output = Math.floor(Math.random() * (input + 1));
    alert(output);
  }
</script>

By using a function, you can call a piece of code multiple times.

Upvotes: 0

Related Questions