window.location
works fine, but returns me the whole, absolute path, like http://domain.xyz/punch/lines
. But I only need http://domain.xyz/
. How can I extract only that first part? And how can I make that dynamic, I mean to be always the same even when the subdirectory path gets longer?
To find the base URL of your website, go to the site's front page. What you see in the address bar on your site's front page is the base URL of your website.
The <base> tag specifies the base URL and/or target for all relative URLs in a document. The <base> tag must have either an href or a target attribute present, or both. There can only be one single <base> element in a document, and it must be inside the <head> element.
Returns the base URL for the object.
Pass the url to the urlparse method from the urllib. parse module. Access the netloc attribute on the parse result.
You can get the protocol and the host separately, and then join them to get what you need
window.location.protocol + "//" + window.location.host + "/"
As a sidenote, window.location.pathname
would contain the path.
You can use this statement
var baseUrl = document.location.origin;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With