A very simple snippet, which stores the root URL in a variable:
[javascript]
// Retrieve root URL
var root = location.protocol + '//' + location.host;
[/javascript]
A very simple snippet, which stores the root URL in a variable:
[javascript]
// Retrieve root URL
var root = location.protocol + '//' + location.host;
[/javascript]
Great and simple solution!
ReplyDeleteHere's a solution to a related issue: Getting the base (path) of the current URL.
You have something like:
http://www.example.com/something/index.html
You want:
http://www.example.com/something/
function getBaseUrl() {
var re = new RegExp(/^.*\//);
return re.exec(window.location.href);
}
More details here: https://404it.no/en/blog/javascript_get_base_url_or_root_url