0

I'm a newbee in javascript and i need to refresh page when script is done:

  <script type="text/javascript">
    function validateForm() {
      var x = document.forms["myForm"]["Hostname"].value;
      if (x == "" || x == null) {
        alert("Name must be filled out");
        return document.location.reload(true);
      }
    }
    </script>

But i don't understand how to make it work.

  • 3
    Does this answer your question? [How to reload a page using JavaScript](https://stackoverflow.com/questions/3715047/how-to-reload-a-page-using-javascript) – Justinas Apr 23 '20 at 07:02
  • can it be used in return: ? I need to reload page if value is empty. Thanks – Explorethetruth Apr 23 '20 at 07:06
  • 1
    You don't need to return it. Maybe your `x` is `undefined`? – Justinas Apr 23 '20 at 07:19
  • maybe this one helps: `if(!x || x.trim() === "")` also `location` doesn't belong to `document`. use directly `location.reload();` – Yasin Okumuş Apr 23 '20 at 07:53
  • – Explorethetruth Apr 23 '20 at 08:32

2 Answers2

0

try it

window.location.reload(true);
Makwana Prahlad
  • 1,025
  • 5
  • 20
0
<script type="text/javascript">
function validateForm() {
  var x = document.forms["myForm"]["Hostname"].value;
  if (x == "" || x == null) {
    alert("Please specify a FQDN of your host");
    window.location.reload(true);
    return false;
 </script>

This one works fine. Thanks to all