如何让IE支持带有相对href的html基本标签?例如:``

发布于 2025-01-04 04:08:17 字数 399 浏览 3 评论 0原文

我知道根据规范,basehref 应该是绝对 URI。

href = uri [CT]
This attribute specifies an absolute URI that acts as the base URI for resolving relative URIs.

但是firefox和chrome支持相对URI,例如../../,这对于我当前的项目非常重要。对于我的项目,除了“相对基本 href”之外,我没有找到更好的解决方案。

IE 是否有任何 hacks 或 workgrounds 可以让它支持相对 URI?我的网页现在在 Firefox 和 Chrome 中运行良好,但它必须支持 IE。

I know according to the spec, the href of base should be a absolute URI.

href = uri [CT]
This attribute specifies an absolute URI that acts as the base URI for resolving relative URIs.

But firefox and chrome support relative URIs, e.g. ../../, which is very important of my current project. I don't find a better solution other than "relative base href" for my project.

Is there any hacks or workgrounds for IE to let it support relative URIs? My web pages works well in firefox and chrome now, but it has to support IE.

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

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

发布评论

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

评论(1

[旋木] 2025-01-11 04:08:17

使用此函数将您的 URL 转换为绝对 URL:

function toAbsURL(s) { 
     var l = location, h, p, f, i; 
     if (/^\w+:/.test(s)) { 
       return s; 
     } 
     h = l.protocol + '//' + l.host + (l.port!=''?(':' + l.port):''); 
     if (s.indexOf('/') == 0) { 
       return h + s; 
     } 
     p = l.pathname.replace(/\/[^\/]*$/, ''); 
     f = s.match(/\.\.\//g); 
     if (f) { 
       s = s.substring(f.length * 3); 
       for (i = f.length; i--;) { 
         p = p.substring(0, p.lastIndexOf('/')); 
       } 
     } 
     return h + p + '/' + s; 
   }

您可以使用

var base = document.getElementsByTagName('base')[0];
base.href = toAbsURL(base.href);

示例 http://jsfiddle.net/tEpkx/1/

除了您还必须检测浏览器,并且仅针对 IE 运行它。其他浏览器将自动通过 base 标记的 href 更新 window.location,此代码片段将再次更改它。所以写成

<!--[if IE]>
<script type="text/javascript">
var base = document.getElementsByTagName('base')[0];
base.href = toAbsURL(base.href);
</script>
<![endif]-->

ps: 是单个标签,不需要结束标签。

更新以包含端口号(如果已设置)。

Use this function to convert your URL to the absolute:

function toAbsURL(s) { 
     var l = location, h, p, f, i; 
     if (/^\w+:/.test(s)) { 
       return s; 
     } 
     h = l.protocol + '//' + l.host + (l.port!=''?(':' + l.port):''); 
     if (s.indexOf('/') == 0) { 
       return h + s; 
     } 
     p = l.pathname.replace(/\/[^\/]*$/, ''); 
     f = s.match(/\.\.\//g); 
     if (f) { 
       s = s.substring(f.length * 3); 
       for (i = f.length; i--;) { 
         p = p.substring(0, p.lastIndexOf('/')); 
       } 
     } 
     return h + p + '/' + s; 
   }

You could use

var base = document.getElementsByTagName('base')[0];
base.href = toAbsURL(base.href);

Example http://jsfiddle.net/tEpkx/1/

Except that you have to detect the browser, too, and run it only for IE. Other browsers will get window.location updated by the href of the base tag automatically and this code snippet will change it again. So write it as

<!--[if IE]>
<script type="text/javascript">
var base = document.getElementsByTagName('base')[0];
base.href = toAbsURL(base.href);
</script>
<![endif]-->

ps: <base /> is a single tag, it does not require the closing one.

Updated to include the port number if it is set.

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