Class Head
<head> element.
The <head> element is a container for document metadata and provides
information about the HTML document that is not displayed directly on the page.
It typically contains the document title, character set declaration, links to
stylesheets and scripts, meta tags for SEO and social media, and other metadata.
The <head> element restricts its content to metadata elements only:
<title>, <base>, <link>, <meta>, <style>,
<script>, <noscript>, and <template>. All valid child types
implement the HeadElement marker interface. Use
invalid reference
#addElement(HeadElement)addMeta(java.lang.String, java.lang.String), addJavaScript(java.lang.String), addCssLink(java.lang.String),
etc.) to add children.
Usage:
// Create a head element with metadata
Head head = new Head();
head.setTitle("My Web Page");
head.setDescription("A description of my page");
head.setKeywords("java, html, web development");
// Add external resources
head.addCssLink("/styles/main.css");
head.addJavaScript("/scripts/app.js");
// Add resources with cache-busting timestamps
head.addCssLink("/styles/theme.css", true);
head.addJavaScript("/scripts/utils.js", true);
// Add custom meta tags
head.addMeta("author", "John Doe");
head.addMeta("viewport", "width=device-width, initial-scale=1.0");
- Since:
- 2007
- Version:
- 1.0
- Author:
- Marvin P. Warble Jr.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal voidaddCssLink(CssLink link) Adds a pre-configuredCssLinkelement to the head.final voidaddCssLink(String url) Adds an external CSS stylesheet link to the head by URL.final voidaddCssLink(String url, boolean includeTimeStamp) Adds an external CSS stylesheet link with optional cache-busting timestamp.final voidaddCssStyleSheet(CssStyleSheet styleSheet) Adds an inline CSS stylesheet to the head.final voidaddJavaScript(JavaScript script) Adds a pre-configuredJavaScriptelement to the head.final voidaddJavaScript(String url) Adds an external JavaScript file to the head by URL.final voidaddJavaScript(String url, boolean includeTimeStamp) Adds an external JavaScript file with optional cache-busting timestamp.final voidAdds a pre-configuredMetaelement to the head.final voidAdds a custom meta tag with the specified attribute name and value.final voidAdds a custom meta tag with an attribute, value, and content.voidRemoves all child elements from this head.final voidSets the Content Security Policy for the document via a<meta>element.final voidSets the Content Security Policy in report-only mode via a<meta>element.final voidsetDescription(String descr) Sets the description meta tag for search engine optimization.final voidsetKeywords(String keywords) Sets the keywords meta tag for search engine optimization.final voidsetOpenGraph(String title, String description, String imageUrl, String pageUrl) Sets the core Open Graph meta tags for social media sharing.final voidsetOpenGraphLocale(String locale) Sets the Open Graph locale meta tag.final voidsetOpenGraphSiteName(String siteName) Sets the Open Graph site name meta tag.final voidsetOpenGraphType(String type) Sets the Open Graph type meta tag.final voidSets the robots meta tag to control search engine crawler behavior.Sets the document title by creating and adding a<title>element.final voidsetTwitterCard(String card, String title, String description, String imageUrl) Sets the core Twitter Card meta tags for Twitter sharing.final voidsetTwitterSite(String site) Sets the Twitter site meta tag.Methods inherited from class com.oorian.html.Element
addAttribute, addAttribute, addAttribute, addAttribute, addElement, addLineBreak, addLineOfText, addLineOfText, addLineOfText, addParagraph, addParagraph, addSpacer, addText, addText, addText, addText, assignId, containsElement, containsElement, create, dispatchEvent, dispatchEvent, dispatchEvent, dispatchEvent, equals, excludeId, executeJs, executeJs, executeJs, getAccept, getAllElements, getAncestor, getAttribute, getAttributes, getComponent, getDir, getElement, getElementById, getElementCount, getElementCount, getElements, getElements, getElementsByComponentName, getElementsByTagName, getHtml, getHtml, getId, getInnerHtml, getInnerHtml, getIs, getItemId, getItemProp, getItemRef, getItemType, getLang, getNextSibling, getPage, getParent, getPart, getPrevSibling, getSlot, getTagName, getTextContent, getUrl, hasAttribute, hasElements, hidden, initialize, insertElement, isChildOf, isClosedTag, isCreated, isDescendantOf, isDescendantOf, isInitialized, isItemScope, isTranslate, onCallback, onCreated, onElementAdded, onElementRemoved, onHashChange, onHidden, onInitialized, onJsReturn, onPageLoaded, onPageUnloaded, onRefresh, onRemovedFromPage, onShown, onUpdated, onUserEvent, prewrite, recreate, refresh, refresh, registerAddition, registerListener, registerListener, registerListener, registerListener, registerListener, registerListener, registerListener, registerSubtraction, registerUpdate, removeAttribute, removeAttribute, removeElement, removeElement, removeFromParent, requestCallback, requestCallback, requestCallback, requestCallback, resetId, scrollTo, scrollToBottom, scrollToTop, self, sendCommand, sendUpdate, setAccept, setComponent, setDir, setDir, setElement, setId, setIs, setItemId, setItemProp, setItemRef, setItemScope, setItemType, setLang, setOnError, setOnLoad, setPage, setParent, setPart, setSlot, setTagName, setText, setText, setText, setText, setTranslate, shown, toString, unregisterListener, update, updateAttributes
-
Constructor Details
-
Head
public Head()Constructs an empty<head>element.Creates a head element for containing document metadata. Child elements such as title, meta tags, links, and scripts should be added using the appropriate setter and add methods.
-
-
Method Details
-
removeAllElements
public void removeAllElements()Removes all child elements from this head.- Overrides:
removeAllElementsin classElement<Head>
-
setTitle
Sets the document title by creating and adding a<title>element.The title appears in the browser tab, bookmarks, and search engine results. This method overrides the parent implementation to add a Title element to the head.
- Parameters:
title- the title text for the document- Returns:
- this Head element for method chaining
-
setKeywords
Sets the keywords meta tag for search engine optimization.Creates a meta element with name="keywords" containing a comma-separated list of keywords relevant to the page content.
- Parameters:
keywords- comma-separated list of keywords for SEO
-
setDescription
Sets the description meta tag for search engine optimization.Creates a meta element with name="description" containing a brief description of the page content, typically displayed in search results.
- Parameters:
descr- the page description for SEO; should be 150-160 characters
-
setRobots
Sets the robots meta tag to control search engine crawler behavior.Creates a meta element with name="robots" to specify directives for search engine crawlers. Common values include "index,follow", "noindex,nofollow", "index,nofollow", etc.
- Parameters:
robots- comma-separated crawler directives (e.g., "index,follow")
-
setOpenGraph
Sets the core Open Graph meta tags for social media sharing.Creates four meta tags with
propertyattributes forog:title,og:description,og:image, andog:url. These are the minimum recommended Open Graph tags for rich link previews on social media platforms such as Facebook, LinkedIn, and others.- Parameters:
title- the page title as it should appear in social media previewsdescription- a brief description of the page contentimageUrl- the URL of the image to display in social media previewspageUrl- the canonical URL of the page
-
setOpenGraphType
Sets the Open Graph type meta tag.Specifies the type of content the page represents. Common values include "website", "article", "product", "profile", and "video.movie".
- Parameters:
type- the Open Graph content type (e.g., "website", "article")
-
setOpenGraphSiteName
Sets the Open Graph site name meta tag.Specifies the name of the overall website or application. This is displayed alongside the page title in social media previews.
- Parameters:
siteName- the name of the website (e.g., "Oorian")
-
setOpenGraphLocale
Sets the Open Graph locale meta tag.Specifies the locale of the page content using a language_TERRITORY format. Defaults to "en_US" if not specified.
- Parameters:
locale- the locale in language_TERRITORY format (e.g., "en_US", "fr_FR")
-
setTwitterCard
Sets the core Twitter Card meta tags for Twitter sharing.Creates four meta tags with
nameattributes fortwitter:card,twitter:title,twitter:description, andtwitter:image. Common card types include "summary", "summary_large_image", "app", and "player".- Parameters:
card- the card type (e.g., "summary_large_image", "summary")title- the title to display in the Twitter carddescription- a brief description of the page contentimageUrl- the URL of the image to display in the Twitter card
-
setTwitterSite
Sets the Twitter site meta tag.Specifies the Twitter handle of the website or organization that owns the content. The handle should include the
@prefix.- Parameters:
site- the Twitter handle of the site (e.g., "@oorian")
-
setContentSecurityPolicy
Sets the Content Security Policy for the document via a<meta>element.Creates a
<meta http-equiv="content-security-policy" content="...">element using the directives configured in the providedContentSecurityPolicybuilder. This enforces the policy and blocks any content that violates it.- Parameters:
csp- the Content Security Policy builder containing the configured directives
-
setContentSecurityPolicyReportOnly
Sets the Content Security Policy in report-only mode via a<meta>element.Creates a
<meta http-equiv="content-security-policy-report-only" content="...">element using the directives configured in the providedContentSecurityPolicybuilder. In report-only mode, violations are reported but not enforced, allowing developers to test a policy before enabling enforcement.- Parameters:
csp- the Content Security Policy builder containing the configured directives
-
addMeta
Adds a custom meta tag with the specified attribute name and value.Creates a meta element with a single attribute-value pair.
- Parameters:
attribute- the attribute name for the meta tagvalue- the value for the attribute
-
addMeta
Adds a custom meta tag with an attribute, value, and content.Creates a meta element with an attribute-value pair and content attribute.
- Parameters:
attribute- the attribute name for the meta tagvalue- the value for the attributecontent- the content value for the meta tag
-
addMeta
Adds a pre-configuredMetaelement to the head.- Parameters:
meta- the meta element to add
-
addJavaScript
Adds an external JavaScript file to the head by URL.Creates a
<script>element linking to the specified JavaScript file.- Parameters:
url- the URL of the JavaScript file; may be absolute or relative
-
addJavaScript
Adds an external JavaScript file with optional cache-busting timestamp.Creates a
<script>element linking to the specified JavaScript file. IfincludeTimeStampis true, appends a query parameter with the current timestamp to prevent browser caching of stale resources.- Parameters:
url- the URL of the JavaScript file; may be absolute or relativeincludeTimeStamp- if true, appends a timestamp to the URL for cache-busting
-
addJavaScript
Adds a pre-configuredJavaScriptelement to the head.- Parameters:
script- the JavaScript element to add
-
addCssLink
Adds an external CSS stylesheet link to the head by URL.Creates a
<link>element referencing the specified CSS file.- Parameters:
url- the URL of the CSS file; may be absolute or relative
-
addCssLink
Adds an external CSS stylesheet link with optional cache-busting timestamp.Creates a
<link>element referencing the specified CSS file. IfincludeTimeStampis true, appends a query parameter with the current timestamp to prevent browser caching of stale resources.- Parameters:
url- the URL of the CSS file; may be absolute or relativeincludeTimeStamp- if true, appends a timestamp to the URL for cache-busting
-
addCssLink
Adds a pre-configuredCssLinkelement to the head.- Parameters:
link- the CSS link element to add
-
addCssStyleSheet
Adds an inline CSS stylesheet to the head.Creates a
<style>element containing the CSS rules from the provided stylesheet object. This embeds the styles directly in the HTML rather than linking to an external file.- Parameters:
styleSheet- the CSS stylesheet to embed inline
-