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

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

问题描述

我正尝试使用< base> TAG来指示包含位于单独文件夹中的html页面的媒体文件的源文件夹。



我有以下文件夹结构:

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

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

 < base href =../ MEDIA_FOLDER/> 

问题是:它适用于某些浏览器(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忽略相对基本标记。
(function(){
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; TAG和本地文件夹路径与Internet Explorer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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