3

I am trying to get my applications root URL from within a .js file. A solution I came upon suggested to use:

window.location.origin

However, because the site is hosted in a virtual directory in IIS, that only returns http://www.whatever.com, but I need it to return http://www.whatever.com/mydir. This might seem like simple string parsing, but I debug using localhost where there is no virtual dir, so I need a solution that is available from with a .js file (not a script block within an HTML document) and is dynamic in that it will work with and without a virtual directory.

2
  • Impossible. How should the client know about the server's internal directory structure? If, however, the url of that page contains the path, have a look at top.location.href Commented Mar 13, 2017 at 16:35
  • Would this work for you (assuming you only want the first directory): window.location.origin + '/' + window.location.pathname.split('/')[1] Commented Mar 13, 2017 at 16:48

1 Answer 1

3

There is no way to do this from client side code.

The browser only knows about URLs. It has no way to know how you used them to organise your site.

If you want to tell the browser the /mydir is special, then you'll need to encode that in the data you send to it (e.g. in a <meta> element).

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.