html的<base>标记与struts的<html:base>

 

HTML元素   <base>

定义
指定一个显式 URL 用于解析对于外部源的链接和引用,如图像和样式表。
Specifies an explicit URL used to resolve links and references to external sources such as images and style sheets.

元素说明
当使用时,base 元素必须出现在文档的 head 内,在任何用于引用外部资源的元素之前。
此元素在 Internet Explorer 3.0 及以上版本的 HTML 中可用,在 Internet Explorer 4.0 及以上版本的脚本中可用。
此元素不改变内容的显示格式。
此元素不需要关闭标签。
When used, the base element must appear within the head of the document, before any elements that refer to an external source.
This element is available in HTML as of Internet Explorer 3.0, and in script as of Internet Explorer 4.0.
This element is not rendered.
This element does not require a closing tag.

示例代码
下面的例子设置了文档的基础 URL 到一个引用文件夹。Internet Explorer 使用 base 元素来解析指向 http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/href_2.html 的链接。

This example sets the base URL of the document to a reference folder. Internet Explorer uses the base element to resolve the link to http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/href_2.asp .

<HTML>
<HEAD>
   <BASE HREF="http://msdn.microsoft.com/workshop/author/dhtml/reference/"/ >
</HEAD>
<BODY>
Click <A HREF="properties/href_2.asp">here</A> to learn about the
href property.
</BODY>
</HTML>

下面的例子表示,如在文档中指定了合法的 base 元素,则从文档中获取基础 URL;反之,返回 null。
This example retrieves the base URL from the document if a valid base element is specified in the document. Otherwise, the function returns null.

<SCRIPT>
function GetBase()
{
     var oBaseColl = document.all.tags('BASE');
     return ( (oBaseColl && oBaseColl.length) ? oBaseColl[0].href :
         null );
}
</SCRIPT>

HTML中的Base标签的使用说明

  使用<base>标签最主要的原因,是为了确保文档中所有的相对URL都可以被分解成正确的文档地址,即使在文档本身被移动或重命名的情况下也可以正确解析。
  在创建文档集合时,使用<base>标签尤为重要。通过在每个文档中放置正确的<base>标签,就可以在目录甚至服务器之 间移动整个文档集合,而不会破坏文档中任何链接。如果想要让用户查询指向一个与主文档URL不同的URL,那么也需要为可搜索文档 (<isindex>)使用<base>标签。
  当文档同事包含<isindex>标签和其他相对URL时,如果相对URL与所希望的索引处理URL无关,那么就可能会出现问题。正因为这个原因,所以请不要在使用<base>标签的可搜索文档中用相对URL来指定文档的查询URL。
  例如:
  <base href="http://www.webjx.com "> 那么,整个页面的相对地址就是在www.webjx.com 底下展开的。不管移动到何处。

Definition and Usage
用法

The base element specifies a base URL for all the links in a page.
使用base元素为页面所有的连接指定一个基础的URL

Differences Between HTML and XHTML
在HTML和XHTML中的区别

In HTML the <base> tag has no end tag.
在HTML中<base>标签不需要结束标签

In XHTML the <base> tag must be properly closed.
在XHTML中<base>标签必须合理关闭

Tips and Notes
注意点

Note: The <base> tag must go inside the head element.
注意:<base>标签必须使用在head元素内

Example
实例

Assume that the absolute address for an image is:
假设有一个图片的绝对地址:
<img src="http://www.w3schools.com/images/smile.gif " />  

Now we insert the <base> tag, which specifies a base URL for all of the links in a page, in the head section of a page:
现在我们插入<base>标签,就可以为页面内的所有连接加上基础URL:

<head>
<base href="http://www.w3schools.com/images/ " />
</head>

When inserting images on the page in the example above, we just have to specify the relative address, and the browser will look for that file using the full URL, "http://www.w3schools.com/images/smile.gif ":
加上之后我们只需要指定相对的地址就可以将URL变为"http://www.w3schools.com/images/smile.gif ":
<img src="smile.gif" />

Required Attributes
必要属性

DTD indicates in which DTD the attribute is allowed. S=Strict, T=Transitional, and F=Frameset.
下表中的DTD列指明了被支持的DTD属性。S=Strict(精确的),T=Transitional(传统的),F=Frameset(框架)

属性描述DTD
hrefURLSpecifies the URL to use as the base URL for links in the page
为页面的所有连接指定基础URL
STF

Optional Attributes
可选属性

属性描述DTD
target_blank
_parent
_self
_top
Where to open all the links on the page. This attribute can be overridden by using the target attribute in each link.
页面中所有连接的打开方式。如果每个连接使用了target属性那么base的target属性就无效了
  • _blank - all the links will open in new windows
    所有的连接将在新窗口打开
  • _self - all the links will open in the same frame they where clicked
    所有的连接将在自身窗口或是框架中打开
  • _parent - all the links will open in the parent frameset
    所有连接在父级框架集中打开
  • _top - all the links will open in the full body of the window
    所有连接在窗口的主体中打开
TF

 

Standard Attributes and Events
标准属性以及事件

NONE

Try-It-Yourself Demos
演示

One target for all links

How to use the <base> tag to let all the links on a page have the same target.
怎样使用<base>标签来让所有连接使用同一个target

struts标记 <html:base>

<html:base/>不是必须的。   
   它将在输出的html文件的head中添加进类似<base    href="http://server:port/requestURI"/ >   
   的东东。   
   浏览器通过base标签来把当前HTML文件中所有的相对URL转换成绝对URL。   
    
   举例如下:   
   MyView.jsp的内容是   
   ...   
    
   <html:base/>   
   </head>   
   <body>   
   <a    href="../anotherPage.html"    id="aLink">相对URL</a>   
   </body>   
   ...   
    
   我们通过在浏览器中输入http://server:port/context/subpath/MyServlet    
   MyServlet的View即是MyView.jsp。   
   那么浏览器将收到如下的内容:   
   ...   
    
   <base    href="http://server:port/context/subpath/MyServlet "    />   
   </head>   
   <body>   
   <a    href="../anotherPage.html"    id="aLink">相对URL</a>   
   </body>   
   ...   
    
   这样当我们点击相对URL时,浏览器将把base与a通过某种方式连接起来得到   
   一个绝对的URL,从而把网页转到这个绝对的URL上去.   
   就上例来说,浏览器得到的连接起来后的URL是:   
  http://server:port/context/subpath/../anotherPage.html    
   正常化后成:   
  http://server:port/context/anotherPage.html    
   于是最终转到的页面就是正常化后的绝对URL了.   
    
   一言以蔽之:   
   浏览器通过Base标签来把当前文档中的相对URL转成绝对的URL.   
    
   如果MyView.jsp的内容是   
   ...   
    
   <base    href="http://www.163.com/news/index.html"/ >   
   </head>   
   <body>   
   <a    href="../anotherPage.html"    id="aLink">相对URL</a>   
   </body>   
   ...       
    
   那么浏览器点击相对URL将转到http://www.163.com/anotherPage.html    
   而不是我们期望的http://server:port/context/anotherPage.html    
   这就是Base的作用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值