0

I am making a game in JavaScript and when the user wins/loses I am prompted them if they would like to play again.

Instead of reseting all the vars, is there a way to just reload the page?

Justin Besteman
  • 378
  • 1
  • 6
  • 17
  • Yes, see http://stackoverflow.com/questions/5294842/refresh-a-page-using-javascript-or-html. – Zak Oct 19 '16 at 13:43
  • For better usability i prefer to reset your form (if exists and possible) `document.getElementById("myForm").reset();` instead of reloading a page – pleinx Oct 19 '16 at 13:45
  • So with the function of reload is there anyway to keep some vars the same? – Justin Besteman Oct 19 '16 at 13:49

4 Answers4

2

location.reload(); reloads the current document.

selami
  • 2,478
  • 1
  • 21
  • 25
2

Sure, use reload:

location.reload();

You can also load other documents with the replace method:

location.replace("/other_page.html")
Uriel
  • 15,579
  • 6
  • 25
  • 46
  • So with the function of reload is there anyway to keep some vars the same? – Justin Besteman Oct 19 '16 at 13:47
  • @JustinBesteman Yes, use GET when reloading the page (use`replace` to the same page with the variables in the URL as GET variables), and load them back (if they exist) when loading up using this https://css-tricks.com/snippets/javascript/get-url-variables/ . URL with GET looks like `you.url.(or.localhost)?var1=value1&var2=value2&...varn=valuen` – Uriel Oct 19 '16 at 13:52
  • Can you go into more detail with this? – Justin Besteman Oct 19 '16 at 14:02
  • Reload using `replace("?score_u1=" + score_u1 + "&score_u2=" + score_u2)` and so on. Then, on the beginning of your code, check if `score_u1` is in the GET variables (using the code from the link above). If it is, load the given value (parse as int the string provided) and use it. – Uriel Oct 19 '16 at 14:06
1

Here you go:

window.location.reload();
gschambial
  • 1,383
  • 2
  • 11
  • 22
1

You can use window.location.reload().

It works like refresh button

travis_91
  • 171
  • 2
  • 13