base Element

Description

The base element sets a base URL for relative links.

A relative link is one that omits the protocol, host, and port parts of the URL and is evaluated against some other URL-either one specified by the base element or the URL used to load the current document.

The base element also specifies how links are opened when a user clicks them, and how the browser acts after a form has been submitted.

base element has two local Attributes

  • href
  • target

An HTML document should contain, at most, one base element.

href Attribute

The href attribute specifies the base URL against which relative URLs in the document will be resolved.

The following code shows the base element and href attribute in use.


<!DOCTYPE HTML>
<html>
<head>
   <title>Example</title>
   <base  href="http://java2s.com/listings/"/>
</head><!-- w  w w  .jav a  2s  .c o m-->
<body>
   <p>This is a test.</p>
   <a href="http://java2s.com">Visit java2s.com</a>
   <a href="page2.html">Page 2</a>
</body>
</html>

Click to view the demo

The code above sets the base URL to http://java2s.com/listings/.

java2s.com is the name of the server, and listings is the directory on the server.

Later in the document, it creates a hyperlink using the relative URL page2.html.

When the user clicks the hyperlink, the browser combines the base URL and the relative URL to create the combined URL http://java2s.com/listings/page2.html.

If you do not use the base element, then the browser will assume that it should resolve any relative links against the URL of the current document.

For example, if you load a document from the URL http://java2s.com/app/mypage.html and it contains a hyperlink with a relative URL of myotherpage.html, then the browser will attempt to load the second page from the fully qualified URL http://java2s.com/app/myotherpage.html.

target Attribute

The target attribute tells the browser how to open URLs.

The values you specify for this attribute represent a browsing context.





















Home »
  HTML CSS »
    HTML »




HTML Introduction
HTML Document
HTML Section
HTML Group Content
HTML Text Marker
HTML Table
HTML Form
HTML Embed