Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get application root URL within .js file

Tags:

javascript

url

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.

like image 627
oscilatingcretin Avatar asked Jul 27 '26 07:07

oscilatingcretin


1 Answers

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).

like image 121
Quentin Avatar answered Jul 29 '26 22:07

Quentin