0

I have this function below to force reload the webpage when you run the file.

The thing is that the function it's supposed to work just once, but everytime I run it works twice.

Some help please with this function:

    function forcedReload(){
        if(!window.location.hash) {
            window.location = window.location + '#';
            window.location.reload(true);

        }
    }

As you can see, the function adds a # at the end of the URL when it's done, but instead of adding one adds two.

4
  • 2
    may be your forcedReload function getting called twice. Commented Jul 10, 2019 at 9:31
  • 2
    window.location = window.location + '#'; - reloads window.location.reload(true); also reloads Commented Jul 10, 2019 at 9:31
  • I checked it a lot of times and it isn't that Commented Jul 10, 2019 at 9:32
  • Why are you doing this in the first place? If you just want to add a hash, change window.location.hash="#" - but what is the usecase??? Commented Jul 10, 2019 at 9:32

1 Answer 1

1

As @mplungjan suggested adding window.location.hash = "#" does the required:

function forcedReload() {
  console.log(window.location.href, window.location.hash);
  if (!window.location.hash) {
    window.location = window.location + "#";
    window.location.hash = "#";
    window.location.reload(true);
  }
}
console.log(window.location.href, window.location.hash);
<button onclick="forcedReload()">refresh</button>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.