link object
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:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Example HTML code 1:
This example illustrates the use of the link element:
|
|||||
<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?
|
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.
|
|||||
<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?
|
Example HTML code 3:
This example shows how to change a style rule in an external style sheet:
|
|||||
<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?
|
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:
|
|||||
<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?
|
Related pages:
External links:
User Contributed Comments