如何在 Javascript 中设置页面的基本 href?

发布于 2024-09-14 21:34:56 字数 115 浏览 4 评论 0原文

我想根据当前主机名在 Javascript 中设置页面的基本 href 属性。我已经生成了可以在不同主机名上查看的 HTML 页面,这意味着生成基本 href 标签在一个主机名中可以工作,但在另一个主机名中则不正确。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

呢古 2024-09-21 21:34:56

正确的方法是根据当前主机名对标记执行 document.write:

正确:

<script type="text/javascript">
document.write("<base href='http://" + document.location.host + "' />");
</script>

此方法在 IE、FF、Chrome 和 Safari 中产生了正确的结果。它产生的(正确的)结果与执行以下操作不同:

不正确:

<script type="text/javascript">
var newBase = document.createElement("base");
newBase.setAttribute("href", document.location.hostname);
document.getElementsByTagName("head")[0].appendChild(newBase);
</script>

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>
夜空下最亮的亮点 2024-09-21 21:34:56

我认为你最好这样做,

    <script type="text/javascript">
        document.head.innerHTML = document.head.innerHTML + "<base href='" + document.location.href + "' />";
    </script>

因为 location.hostname 不会返回应用程序上下文根!您还可以在控制台 console.log 上记录 document.location,以查看 document.location 上的所有可用元数据。

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.

初懵 2024-09-21 21:34:56

我不得不不同意最上面的答案。它不考虑协议,因此会失败。

我必须考虑协议/主机/端口的工作解决方案如下,

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

目前在包括 IE11 在内的所有主要浏览器中都可以正常工作

我已经使用它来制作一个 npm 包,该包还支持在该基本 href 的末尾添加后缀如果任何人都感兴趣

https://www.npmjs.com/package/dynamic-base

https://github.com/codymikol/dynamic-base

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

https://www.npmjs.com/package/dynamic-base

https://github.com/codymikol/dynamic-base

凑诗 2024-09-21 21:34:56

document.write("");

<script>
document.write("<base href='"+ window.location.protocol +'//' + window.location.host + "' >");
</script>

document.write("");

<script>
document.write("<base href='"+ window.location.protocol +'//' + window.location.host + "' >");
</script>

无声无音无过去 2024-09-21 21:34:56
window.document.head.innerHTML=`<base href=${href}>`+window.document.head.innerHTML;

href 变量是您要使用的 href。

window.document.head.innerHTML=`<base href=${href}>`+window.document.head.innerHTML;

The href variable is the href you want to use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文