How to get the value of the type attribute of a link in JavaScript?

To get the value of the type attribute of a link in JavaScript, use the type property. The type attribute specifies the MIME type of the linked document, such as "text/html" for HTML documents or "text/css" for stylesheets.

Syntax

element.type

Example: Getting Link Type Attribute

You can access the type attribute of a link using getElementById() and the type property:




   

TutorialsPoint

Value of type attribute: text/html

Common Type Attribute Values

Here are typical MIME type values used with links:




   

HTML Link

CSS Link

JS Link

HTML link type: text/html
CSS link type: text/css
JS link type: application/javascript

Checking if Type Attribute Exists

Not all links have a type attribute. You should check if it exists before accessing it:




   

Link with type

Link without type

withType type: text/plain
withoutType has no type attribute

Conclusion

Use the type property to get the MIME type of a link element. Always check if the attribute exists before using it, as not all links include this optional attribute.

Updated on: 2026-03-15T23:18:59+05:30

609 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements