I want to set a page's base href attribute in Javascript based off of the current hostname. I have generated HTML pages that can be viewed on different hostnames, which means generating a base href tag will work in one hostname but will be incorrect in the other.
The correct way of doing this is to do a document.write of the tag based off the current hostname:
Correct:
<script type="text/javascript">
document.write("<base href='http://" + document.location.host + "' />");
</script>
This method has produced correct results in IE, FF, Chrome, and Safari. It produces a (correct) different result than doing the following:
Incorrect:
<script type="text/javascript">
var newBase = document.createElement("base");
newBase.setAttribute("href", document.location.hostname);
document.getElementsByTagName("head")[0].appendChild(newBase);
</script>
document.write
would be “correct” and proper insertion of a node in the document tree would be “incorrect”. The question falls into the category “solving the wrong problem”, but the approach described as “correct” here would often insert a base
tag in a wrong place, to begin with. If the code is in the head
part, it’s rather useless (why not put the right base
tag there as normal HTML?), and if it is executed elsewhere, it will usually insert base
into body
. –
Rotary http:
be document.location.protocol
? –
Risinger document.location.protocol + '//' + document.location.hostname + ( document.location.port ? ':' : '' ) + document.location.port + document.location.pathname + '" />' + document.head.innerHTML
- this makes it more portable, for running on a specific port, such as in Webpack. this fixed a problem with Webpack for me, where Webpack can't write a different path for images in CSS than for images in HTML (they're all relative paths or all absolute paths, but not both, which prevents the app from being portable.) –
On <base>
tag like normally. In fact, if you run the JS code in the devtool's console, it'll empty the entire DOM leaving just the base tag! The real correct method would be to use the document.head.innerHTML = "<base href=''>" + document.head.innerHTML
as it ensures every element uses the new baseHref. –
Tack I think you'd better do it this way
<script type="text/javascript">
document.head.innerHTML = document.head.innerHTML + "<base href='" + document.location.href + "' />";
</script>
As location.hostname
does not return the application context root! You could also log the document.location
on the console console.log
to see all available metadata on document.location
.
document.write
). On a practical note: this worked for me in jsFiddle while document.write
didn't. –
Hacker document.head.innerHTML = "<base href=''>" + document.head.innerHTML
(base first, then innerHTML) for two reasons: 1. Any <link> tags, <script> tags, etc in the <head> making use of the relative path, won't be able to since the <base> is being appended below them. 2. If you execute the code twice or more, the most recent execution becomes the <base> tag that works (because it's whichever is topmost that works and this code "prepends"). –
Tack I have to disagree with the top answer. It does not account for the protocol so it will fail.
A working solution that I have to account for protocol / host / port is the following
var base = document.createElement('base');
base.href = window.location.protocol + '//' + window.location.hostname + (window.location.port ? ':' + window.location.port : '');
document.getElementsByTagName('head')[0].appendChild(base);
This currently works fine in all major browsers including IE11
I have used this to make an npm package that also supports adding a suffix to the end of this base href if anyone is interested
<base>
tag allows relative paths... this is pointless... –
Tack document.baseUri
or inspect the properties of any element, you can see the baseHref
property for each element on the DOM. The base href will be a "fully qualified url" based on the relative path you input into the <base href="">
tag. –
Tack <script>
document.write("<base href='"+ window.location.protocol +'//' + window.location.host + "' >");
</script>
window.document.head.innerHTML=`<base href=${href}>`+window.document.head.innerHTML;
The href variable is the href you want to use.
© 2022 - 2024 — McMap. All rights reserved.
$.serverRoot
to be the relative root of the host (e.g.$.serverRoot = '/myapp/';
– Femininity