Class Link<T extends Link<T>>
- All Implemented Interfaces:
HeadElement
- Direct Known Subclasses:
CssLink
<link> element for external resource linking.
The <link> element defines the relationship between the current document
and an external resource. It is most commonly used to link to external stylesheets,
but can also specify favicons, alternate document versions, prefetch resources,
and other document metadata. The link element is a self-closing tag that appears
in the document head section.
Features:
- Links to external stylesheets and resources
- Specifies document relationships and metadata
- Supports media queries for responsive stylesheets
- Can define favicons and touch icons
- Supports CORS and preloading configurations
Usage:
// Link to a stylesheet
Link cssLink = new Link("/styles/main.css", Relationship.STYLESHEET, "text/css");
// Specify a favicon
Link favicon = new Link("/favicon.ico", "icon", "image/x-icon", "16x16");
// Responsive stylesheet with media query
Link responsiveLink = new Link();
responsiveLink.setHref("/styles/mobile.css");
responsiveLink.setRel(Relationship.STYLESHEET);
responsiveLink.setMedia("screen and (max-width: 600px)");
// Alternate language version
Link alternate = new Link();
alternate.setHref("/es/page.html");
alternate.setRel("alternate");
alternate.setHrefLang("es");
- Since:
- 2007
- Version:
- 1.0
- Author:
- Marvin P. Warble Jr.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionLink()Constructs an empty<link>element.Link(String href, Relationship rel, String mediaType) Constructs a<link>element with href, relationship, and media type.Constructs a<link>element with href and relationship.Constructs a<link>element with href, relationship, and sizes.Constructs a<link>element with href, relationship, media type, and sizes. -
Method Summary
Modifier and TypeMethodDescriptionfinal TSets the resource type hint for preloading.final TsetBlocking(String blocking) Sets the blocking attribute indicating that rendering should be blocked until this resource is fetched.final TsetCrossOrigin(CrossOrigin crossOrigin) Sets the cross-origin attribute using theCrossOriginenum.final TsetCrossOrigin(String crossOrigin) Sets the crossorigin attribute.final TsetDisabled(boolean disabled) Sets the disabled boolean attribute.final TsetFetchPriority(String fetchPriority) Sets the fetchpriority attribute.final TSets the URL of the linked resource.final TsetHrefLang(String languageCode) Sets the hreflang attribute.final TsetImageSizes(String imageSizes) Sets the image sizes attribute for preloading responsive images.final TsetImageSrcSet(String imageSrcSet) Sets the imagesrcset attribute for preloading responsive images.final TsetIntegrity(String integrity) Sets the subresource integrity hash for verifying the fetched resource has not been tampered with.final TSets the media query that the linked resource applies to.final TsetReferrerPolicy(String referrerPolicy) Sets the referrerpolicy attribute.final TsetRel(Relationship rel) Sets the rel attribute using theRelationshipenum.final TSets the rel attribute.Sets the sizes attribute specifying icon sizes.final TSets the MIME type of the linked resource.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, removeAllElements, 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
-
Link
public Link()Constructs an empty<link>element.Creates a link element without attributes. The href, rel, and other attributes should be set using the appropriate setter methods. This constructor excludes the id attribute by default.
-
Link
Constructs a<link>element with href, relationship, and media type.- Parameters:
href- the URL of the linked resourcerel- the relationship type (typically STYLESHEET)mediaType- the MIME type of the linked resource
-
Link
Constructs a<link>element with href, relationship, media type, and sizes.This constructor is useful for specifying icons with specific dimensions.
- Parameters:
href- the URL of the linked resourcerel- the relationship type as a stringmediaType- the MIME type of the linked resourcesizes- the sizes specification (e.g., "16x16", "32x32")
-
Link
Constructs a<link>element with href, relationship, and sizes.- Parameters:
href- the URL of the linked resourcerel- the relationship type as a stringsizes- the sizes specification (e.g., "16x16", "32x32")
-
Link
Constructs a<link>element with href and relationship.- Parameters:
href- the URL of the linked resourcerel- the relationship type as a string
-
-
Method Details
-
setAs
Sets the resource type hint for preloading.Used with
rel="preload"orrel="modulepreload"to specify the type of resource being loaded. This helps the browser prioritize and handle the resource correctly.- Parameters:
as- the resource type (e.g., "script", "style", "image", "font", "fetch")- Returns:
- this element for method chaining
-
setBlocking
Sets the blocking attribute indicating that rendering should be blocked until this resource is fetched.The value
"render"prevents the browser from rendering content until the resource is loaded.- Parameters:
blocking- The blocking token(s).- Returns:
- This element for method chaining.
-
setCrossOrigin
Sets the cross-origin attribute using theCrossOriginenum.- Parameters:
crossOrigin- The CORS mode.- Returns:
- This element for method chaining.
-
setCrossOrigin
Sets the crossorigin attribute.Specifies how the element handles cross-origin requests. The value
"anonymous"sends requests without credentials;"use-credentials"includes cookies and authentication.- Parameters:
crossOrigin- The cross-origin value (anonymous, use-credentials).- Returns:
- This element for method chaining.
-
setDisabled
Sets the disabled boolean attribute.When present on a
<link>element for a stylesheet, the stylesheet will not be loaded and applied to the document. A disabled stylesheet can be enabled later via JavaScript.- Parameters:
disabled- True to disable, false to enable.- Returns:
- This element for method chaining.
-
setFetchPriority
Sets the fetchpriority attribute.Provides a hint to the browser about the relative priority of fetching this resource. Values include
"high","low", and"auto"(default).- Parameters:
fetchPriority- The fetch priority (high, low, auto).- Returns:
- This element for method chaining.
-
setHref
Sets the URL of the linked resource.If the URL starts with "/" (absolute path within the application), the servlet context path will be automatically prepended.
- Parameters:
url- The URL of the linked resource.- Returns:
- This element for method chaining.
-
setHrefLang
Sets the hreflang attribute.Specifies the language of the linked resource as a BCP 47 language tag (e.g.,
"en","fr-CA"). This is advisory only and does not affect browser behavior.- Parameters:
languageCode- The language of the linked resource.- Returns:
- This element for method chaining.
-
setMedia
Sets the media query that the linked resource applies to.The resource is only used when the media query matches. Common values include
"screen","print", and responsive breakpoints.- Parameters:
mediaQuery- The media query for the resource.- Returns:
- This element for method chaining.
-
setRel
Sets the rel attribute using theRelationshipenum.- Parameters:
rel- The link relationship.- Returns:
- This element for method chaining.
-
setRel
Sets the rel attribute.Specifies the relationship between the current document and the linked resource. Common values include
"stylesheet","icon","preload","preconnect", and"dns-prefetch".- Parameters:
rel- The relationship of the linked resource.- Returns:
- This element for method chaining.
-
setSizes
Sets the sizes attribute specifying icon sizes.Used to specify the sizes of icons when used with
rel="icon". The value is a space-separated list of sizes (e.g.,"16x16","32x32","any").- Parameters:
sizes- The icon sizes specification.- Returns:
- This element for method chaining.
-
setImageSizes
Sets the image sizes attribute for preloading responsive images.Used with
rel="preload"andas="image"in conjunction withsetImageSrcSet(String). Specifies image sizes for different viewport conditions.- Parameters:
imageSizes- the sizes descriptor (e.g., "(max-width: 600px) 100vw, 50vw")- Returns:
- this element for method chaining
-
setImageSrcSet
Sets the imagesrcset attribute for preloading responsive images.Used with
rel="preload"andas="image"in conjunction withimagesizes.- Parameters:
imageSrcSet- The image source set for preloading.- Returns:
- This element for method chaining.
-
setIntegrity
Sets the subresource integrity hash for verifying the fetched resource has not been tampered with.The browser verifies the fetched resource matches the hash before using it, protecting against CDN tampering.
- Parameters:
integrity- The subresource integrity hash.- Returns:
- This element for method chaining.
-
setReferrerPolicy
Sets the referrerpolicy attribute.Specifies how much referrer information to send when fetching this resource. Common values include
"no-referrer","origin", and"strict-origin-when-cross-origin".- Parameters:
referrerPolicy- The referrer policy value.- Returns:
- This element for method chaining.
-
setType
Sets the MIME type of the linked resource.Specifies the media type of the linked content (e.g.,
"text/css","image/x-icon"). Helps the browser determine how to handle the resource.- Parameters:
mediaType- The MIME type of the linked resource.- Returns:
- This element for method chaining.
-