1

The following is not working (I just get a reload):

            location.host += "/#settings";
            location.reload();

How do I tell JS to navigate to a URL based on the existing url and totally reload that page (not just append the hash)?

This also does not work:

             window.location.href = "/#settings";

My base url is "http://localhost:sss/pricing"

I want to redirect to "http://localhost:sss/#settings" with a reload.

I don't want to type localhost anywhere.

Even this gives me settings#lol:

            var desiredBase = window.location.protocol + "//" + window.location.host + "/";
            var path = '#lol';
            window.location.href = desiredBase + path;
            location.reload();
SB2055
  • 12,272
  • 32
  • 97
  • 202

3 Answers3

0

Yet another edit, stole base url code from here: How to get base url with jquery or javascript?

<html>

  <body>
    <h1>Hello Plunker!</h1>
    <script>
      (function(){
        var getUrl = window.location;
        var desiredBase = getUrl.protocol + "//" + getUrl.host + "/" + getUrl.pathname.split('/')[1];
        var path = `#lol`;      
        window.location.href = desiredBase + path; 
        location.reload();
      })();
    </script>
  </body>

</html>

This is reloading the page locally after appending. Try it.

Edit: I see the issue with not getting the base url with href, working on it, hold on.

Community
  • 1
  • 1
VSO
  • 11,546
  • 25
  • 99
  • 187
-1

Try this

location.href += "/#settings";
location.reload();
Vololodymyr
  • 1,996
  • 5
  • 26
  • 45
-2

You can try something like this :

location.hash = "settings";

This sets part after "#" in the URL

then do: location.reload(); and it should fix the issue

Let me know if it works

Thanks