Is the HTML <base> tag also honored by scripting and CSS?

一世执手 提交于 2019-11-29 15:55:22

问题


The base HTML element provides a base for relative URIs in the HTML. Must JavaScript and CSS also honor it for relative URIs issued in them:

E.g.

JavaScript:

location.href = "mypage.htm"` 

CSS:

h4 { 
    background-image: url(myimage.gif) 
}

(in any browser?)


回答1:


CSS paths are always (except when IE6 is buggy and stupid and tries to load .htc files specified in CSS behavior attributes relative to the document) relative to the stylesheet itself and have no dependence on the HTML location. For other stuff, <base> will affect the perceived current directory of the HTML as if the file was located in the directory defined by base. Consequently, it does affect things like location.href=...;. By the way, inline styles and style information in <style> element are loaded relative to the document location. Those are affected by the <base> tag, of course.




回答2:


The base tag is indeed only honoured by the relative links inside the HTML document itself.

There's however an IE6-specific bug which you really need to take into account when using <base> tag in HTML (not in XHTML). The <base> tag is in HTML documented as not having an end tag </base>, but IE6 incorrectly assumed it for true which will cause that the entire content after the <base> tag is placed as child of the <base> tag in its HTML DOM tree. This can cause at first sight unexplainable problems in Javascript/jQuery/CSS, i.e. the elements being completely unreachable in specific selections (e.g. html>body) until you discover that there's actually a base in between.

A normal IE6 fix is using conditional comments to include the end tag:

<base href="http://example.com/"><!--[if lte IE 6]></base><![endif]-->


来源:https://stackoverflow.com/questions/2161377/is-the-html-base-tag-also-honored-by-scripting-and-css

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!