HTML | DOM Link Object
HTML DOM Link Object is used to access HTML <link> element.
Syntax:
document.getElementById("myLink");
document.getElementById("myLink");
Propertie Values:
| Value | Description |
|---|---|
| charset | It assigns the character encoding of the linked document |
| crossOrigin | It assigns the the CORS settings of the linked document |
| disabled | It assigns whether the linked document is disabled, or not |
| href | It is used to set/return the URL of the linked document |
| hreflang | It assigns the language code of the linked document |
| media | It assigns the media type for the link element |
| rel | It assigns the relationship between the current document and the linked document |
| rev | It assigns the reverse relationship from the linked document to the current document |
| sizes | Returns the sizes attribute’s value of the linked resource |
| type | It is used to set/return the content type of the linked document |
Example-1: Accessing link element.
<!DOCTYPE html> <html> <head> <link id="linkid" rel="stylesheet" type="text/css" href="styles.css"> </head> <body> <h1>TO ACCESS LINK ELEMENT:</h1> <p>PRESS THE BUTTON TO GET THE URL OF THE LINKED DOCUMENT.</p> <button onclick="gfg()">Get URL </button> <p id="pid"></p> <script> function gfg() { // Access link element. var NEW = document.getElementById( "linkid").href; document.getElementById( "pid").innerHTML = NEW; } </script> </body> </html> |
chevron_right
filter_none
Output:
Before clicking:

After clicking:

Example-2: Create link element.
<!DOCTYPE html> <html> <head> </head> <body> <h1>TO CREATE A LINK ELEMENT.</h1> <button onclick="myFunction()">Create</button> <p id="pid"></p> <script> function myFunction() { // Create link element. var NEW = document.createElement( "LINK"); // set attributes. NEW.setAttribute("id", "linkid"); NEW.setAttribute("rel", "stylesheet"); NEW.setAttribute("type", "text/css"); NEW.setAttribute("href", "styles.css"); document.head.appendChild(NEW); var NEW1 = document.getElementById( "linkid").href; document.getElementById("pid").innerHTML = NEW1; } </script> </body> </html> |
chevron_right
filter_none
Output:
Before clicking:

After clicking:

Supported Browsers:
- Chrome
- Firefox
- Interent Explorer
- Safari
- Opera
Recommended Posts:
- HTML | <link> Tag
- HTML | <link> rel Attribute
- HTML | DOM Link rel Property
- HTML | <link> hreflang Attribute
- HTML | <link> sizes Attribute
- HTML | DOM Link type Property
- HTML | <link> target Attribute
- How to make a call-able link using HTML ?
- HTML | DOM Link sizes Property
- HTML | <link> href Attribute
- HTML | DOM Link disabled Property
- HTML | <link> media Attribute
- HTML | <link> charset Attribute
- HTML | <body> link Attribute
- HTML | DOM Link href Property
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



