HTML <base>使用 Internet Explorer 标记和本地文件夹路径 [英] HTML <base> TAG and local folder path with Internet Explorer

查看:28
本文介绍了HTML <base>使用 Internet Explorer 标记和本地文件夹路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 <base> 标记表示包含我的 html 页面的媒体文件的源文件夹,位于单独的文件夹中.

我有以下文件夹结构:

A|- HTML_PAGES(包含 html 文件)|- MEDIA_FOLDER(包含此html页面使用的媒体)

我尝试用 html 页面使用的媒体来指示 html 文件 - 因此,在每个 html 文件中,我都有这样的内容:

问题是:它适用于某些浏览器(Opera、Chrome),但不适用于 Internet Explorer 和 Firefox.如何让它在 IE 和 Firefox 下工作?

解决方案

这绝对是 IE 中一个非常烦人的错误,但我刚刚找到了一个解决方法.

要意识到的是,IE 确实解析了相对路径,然后立即忽略了它.您甚至可以在稍后使用 JavaScript 时通过检查基本标记的href"属性的值来查看完全解析的 URL.因此,这段(相当愚蠢的)代码将 <base> 标记的href"属性重置为 IE 已经解析的非常完整的 URL,从而使其不再被忽略.

将以下 HTML 添加到页面顶部,紧跟在标记之后且在您实际使用任何 URL 之前:

<!--[if IE]><script type="text/javascript">//修复 IE 忽略相对基础标签的问题.(功能() {var baseTag = document.getElementsByTagName('base')[0];baseTag.href = baseTag.href;})();</script><![endif]-->

(需要条件注释,因为此代码可以破坏 Safari/Chrome 中的 标记,而其他浏览器显然不需要它.)

这种愚蠢的错误.

I am trying to use < base> TAG to indicate the source folder containing the media files for my html pages located in separate folder.

I have the following folder structure:

A
|- HTML_PAGES        (contains html files)
|- MEDIA_FOLDER      (contains the media used by this html pages)

I try to indicate the html files with the media used by html pages - so, in each html file i have something like this:

<base href="../MEDIA_FOLDER"/>

And the problem is: it works for some browsers (Opera, Chrome) but it doesn't work for Internet Explorer and Firefox. How to make it work with IE and Firefox?

解决方案

This is definitely a very annoying bug in IE, but I just found a workaround.

The thing to realize is that IE does resolve the relative path, and then promptly ignores it. You can even see the fully resolved URL by checking the value of the base tag's 'href' property later on using JavaScript. So this (rather silly) piece of code resets the <base> tag's 'href' attribute to the very full URL that IE had already resolved, thereby causing it to no longer be ignored.

Add the following HTML to the top of your page, right after the tag and before you actually use any URLs:

<!--[if IE]><script type="text/javascript">
    // Fix for IE ignoring relative base tags.
    (function() {
        var baseTag = document.getElementsByTagName('base')[0];
        baseTag.href = baseTag.href;
    })();
</script><![endif]-->

(conditional comments necessary since this code can break the <base> tag in Safari/Chrome, and other browsers clearly don't need it.)

Such a silly bug.

这篇关于HTML &lt;base&gt;使用 Internet Explorer 标记和本地文件夹路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆