What are the recommendations for html <base> tag?

前端 未结 17 2258
一向
一向 2020-11-21 10:17

I\'ve never seen HTML tag actually used anywhere before. Are there pitfalls to its use that means I should avoid it?

The fact that I have never noticed

17条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-21 10:38

    Before deciding whether to use the tag or not, you need to understand how it works, what it can be used for and what the implications are and finally outweigh the advantages/disadvantages.


    The tag mainly eases creating relative links in templating languages as you don't need to worry about the current context in every link.

    You can do for example

    
    ...
    
    
    ...
    home
    faq
    contact
    ...
    
    

    instead of

    
    
    ...
    home
    faq
    contact
    ...
    
    

    Please note that the value ends with a slash, otherwise it will be interpreted relative to the last path.


    As to browser compatibility, this causes only problems in IE. The tag is in HTML specified as not having an end tag , so it's legit to just use without an end tag. However IE6 thinks otherwise and the entire content after the tag is in such case placed as child of the element in the HTML DOM tree. This can cause at first sight unexplainable problems in Javascript/jQuery/CSS, i.e. the elements being completely unreachable in specific selectors like html>body, until you discover in the HTML DOM inspector that there should be a base (and head) in between.

    A common IE6 fix is using an IE conditional comment to include the end tag:

    
    

    If you don't care about the W3 Validator, or when you're on HTML5 already, then you can just self-close it, every webbrowser supports it anyway:

    
    

    Closing the tag also instantly fixes the insanity of IE6 on WinXP SP3 to request

提交回复
热议问题