I have been (mis)using the title
attribute of stylesheets to identify and grab a specific one for further processing with my JavaScript. I did this by comparing stylesheet.title
with a specified title for my embedded stylesheet. However, as I didn’t really intend the titled stylesheet to be a preferred one, once I added a title to another stylesheet higher up in the document, my other one (being lower in the document hierarchy) stopped working.
This lead me to investigate the real significance of title
, when applied to stylesheets. From what I’ve read, I have to say I’m somewhat confused by the logic with which the attribute has been named. After all, a title
applied or left unapplied, to just about anything else on a web page is completely harmless, save perhaps for some decrease in the fine-tuning of your site’s accessibility. A title
with relation to a stylesheet however, is so significant that HTML 4.01 categorizes stylesheets according to the presence or absence of a title
. Although I don’t claim to know anything about writing specifications, I think they should have associated this categorization with the rel
attribute, or at least name it differently so its significance wouldn’t get blurred by the use of title
elsewhere. A stylesheet’s title
‘s role would then reduced to accessory information similar to what it is when associated with, say, an a
element.
Anyway, I’ve now come up with what I at least think is a better way of identifying my embedded stylesheet. I tag the style
element with an id
attribute, and then compare stylesheet.ownerNode.id
with whatever the string I’ve tagged it with is. I’m not sure if it’s the correct way or even whether there is one, but it seems to be working and is valid. From the specification I gather there is no id
attribute associated with style
in HTML 4, so it probably wouldn’t validate there. This isn’t a problem for me, since I nearly always use XHTML (Strict).