You are here: Reference > JavaScript > client-side > HTML DOM > objects > nodes and tags > link

link object

Browser support:
Specifies a link to an external resource.
This element must be placed in the head section.
The link element allows authors to link external documents into the HTML document. Usually it is used to link external .css (style) files into the document. Another common use of the link tag is to identify an own or a specific search provider, as shown in Example 2.
If you want to import a CSS file into another CSS file, use the @import at-rule.
If you need to modify the contents of an external style sheet dynamically, use the styleSheet object and the cssRules and rules collections. For further details, see the examples below.
To install a search provider from JavaScript, use the AddSearchProvider method.

Syntax:

Methods that return the object:
document.createElement ("link")
The base interface, through which you can add new functionalities to the link object, is the HTMLLinkElement interface.
If you want to see the HTML objects by categories, please visit this page.
HTML page for this element: link

Possible members:

Properties
Methods
Events
Style properties
attributes
Represents a collection of attribute nodes that belong to an element.
baseURI
10
Returns the base URL for the object.
behaviorUrns
Represents a collection of the Uniform Resource Names for all behaviors attached to an element.
canHaveChildren
Retrieves a Boolean value that indicates whether the element can contain child elements.
canHaveHTML
Retrieves a Boolean value that indicates whether the element can contain HTML formatted text.
charset
Sets or retrieves the character encoding of the document, a linked document or a script block.
className
Sets or retrieves the style class or classes that belong to the element.
currentStyle
Represents the computed style settings for an element.
dir
Sets or retrieves the text direction as related to the lang property.
disabled
Sets or retrieves the state of an object for user interaction.
href
Specifies or returns the location of the destination.
hreflang
Sets or retrieves the language of a linked resource or anchor element.
id
Sets or retrieves a unique identifier for the object.
isContentEditable
Returns a Boolean value that indicates whether the contents of the object are editable by the user.
isDisabled
Returns a Boolean value that indicates whether the object is disabled.
isMultiLine
Returns a Boolean value that indicates whether the contents of an element can be multiline or not.
isTextEdit
Returns a Boolean value that indicates whether the createTextRange method can be used for the element.
lang
Specifies or returns the language of the element.
localName
9
Returns the local part of the qualified name of the current node.
media
Specifies or returns the media types for style definition.
name
Sets or retrieves the name of an element.
namespaceURI
93.6
Sets or returns the namespace URI of the current node.
nextElementSibling
93.5
Returns a reference to the next child element of the current element's parent.
nextSibling
Returns a reference to the next child of the current element's parent.
nodeName
Returns the name of the current node.
nodeType
Returns an integer that indicates the type of the node.
nodeValue
Sets or returns the value of the current node.
outerHTML
Sets or retrieves the outer HTML content (the source code including the opening and closing tags) of an element.
outerText
Sets or returns the text content of an element including the text content of its descendants.
ownerDocument
Returns the document object that contains the current node.
parentElement
Returns the parent element of the object in the DOM hierarchy.
parentNode
Returns the parent element of the current node in the DOM hierarchy.
parentTextEdit
Returns the closest ancestor element of the current element in the DOM hierarchy that can be used to create a TextRange object.
previousElementSibling
93.5
Returns a reference to the previous child element of the current element's parent.
previousSibling
Returns a reference to the previous node of the current element's parent.
readyState
Returns a string value that represents the state of the object.
rel
Specifies or returns the relationship between the current document and the destination of an anchor or link.
rev
Specifies or returns a reverse relationship from an anchor or link to the current document.
runtimeStyle
Represents the overridden style settings for an element.
scopeName
Retrieves the local name of the namespace declared for the current element.
sheet
9
Represents a CSS style sheet that belongs to a style element or an imported style file.
sourceIndex
Returns the position of the current object in the all collection of the document.
style
Represents the inline style settings for an element or a CSS rule.
styleSheet
Represents a CSS style sheet that belongs to a style element or an imported style file.
tagName
Returns the tag name of the current element.
tagUrn
Sets or retrieves the Uniform Resource Name (URN) of the namespace declared for the current element.
target
Specifies or returns the target window or frame where the document is to be opened.
title
Specifies or returns a tooltip for an element.
type
Specifies or returns the content type (MIME type) of the object or the linked object.
uniqueID
Returns the unique identifier generated by the browser for the object.

Example HTML code 1:

This example illustrates the use of the link element:
Code
red.css
<head>
    <link rel="stylesheet" type="text/css" href="red.css" />
</head>
<body>
    <div class="red">red division</div>
    <div>non-red division</div>
    <span class="red">red span</span>
</body>
Did you find this example helpful? yes no

Example HTML code 2:

This example shows how to tell the client browser that you offer a search plugin from HTML. If the search provider is currently installed, it will be selected search engine. Note that the AddSearchProvider method provides similar functionality in JavaScript.
Code
searchProvider.xml
<head>
    <link rel="search" type="application/opensearchdescription+xml" 
        href="searchProvider.xml" title="Dottoro Search" /> 
</head>
<body>
    Please open the drop-down menu of your browser's Search Box.
</body>
Did you find this example helpful? yes no

Example HTML code 3:

This example shows how to change a style rule in an external style sheet:
Code
red.css
<head>
    <link rel="stylesheet" type="text/css" href="red.css" />
    <script type="text/javascript">
        function ChangeRedColor () {
                // the imported style sheet
            var importedSheet = document.styleSheets[0];
    
                // the first rule in the style sheet
            var rules = importedSheet.cssRules ? importedSheet.cssRules : importedSheet.rules;
            var firstRule = rules[0];

                // change the color
            firstRule.style.color = "#00ff00";
        }
    </script> 
</head>
<body>
    <button onclick="ChangeRedColor ()">Change the color of the '.red' style rule!</button>
    <br /><br />
    <div class="red">red division</div>
    <div>non-red division</div>
    <span class="red">red span</span>
</body>
Did you find this example helpful? yes no

Example HTML code 4:

This example is the same as the previous one, but it uses the sheet and styleSheet properties of the link element to get the style sheet:
Code
red.css
<head>
    <link id="myLink" rel="stylesheet" type="text/css" href="red.css" />
    <script type="text/javascript">
        function ChangeRedColor () {
            var linkTag = document.getElementById ("myLink");

                // the imported style sheet
            var importedSheet = linkTag.sheet ? linkTag.sheet : linkTag.styleSheet;
    
                // the first rule in the style sheet
            var rules = importedSheet.cssRules ? importedSheet.cssRules : importedSheet.rules;
            var firstRule = rules[0];

                // change the color
            firstRule.style.color = "#00ff00";
        }
    </script> 
</head>
<body>
    <button onclick="ChangeRedColor ()">Change the color of the '.red' style rule!</button>
    <br /><br />
    <div class="red">red division</div>
    <div>non-red division</div>
    <span class="red">red span</span>
</body>
Did you find this example helpful? yes no

Related pages:

External links:

User Contributed Comments

Post Content

Post Content