4

I'm looking for good/semantic/passing-validation way to indicate my links, that are used as hooks for javascript.

<a href="somelink" rel="js">link</a>

As I understand "rel" is more about relationships between documents. Also the "data-lang" (from here) doesn't feel to be a good enough solution.

Thanks a lot in advance!

sdvig
  • 123
  • 1
  • 6
  • 2
    The question is: would your links work without the JavaScript (so, with JavaScript they are only "enhanced", for example allowing to partly reload the page, instead of full refresh) or are they "pseudo-links" and they are only there for JS-enabled functionality? – Misha Reyzlin Aug 21 '11 at 11:39
  • @gryzzly Currently I have only "pseudo-links", but I would like to hear the solutions for both scenarios :) – sdvig Aug 21 '11 at 12:16

2 Answers2

2

You may use the class-attribute, or as of HTML5, the custom data-attributes.

feeela
  • 26,359
  • 6
  • 56
  • 68
  • 1
    The question is not about how to pass data to JavaScript. It's about how to show in markup that these links are not links to web-resources or internal anchors but merely JavaScript triggers. – katspaugh Aug 21 '11 at 12:16
1

If the link is an "enhanced" link, that will do pretty much the same that the usual link would do but in a more "user-friendly" way – such as a navigation link, that, when JavaScript is enabled on the page, will reload only the "content" part of the page and update the address of the page with HTML5 History API – then I would just use a semantic class, actually describing the links, such as "navigation".

In case they are JavaScript triggers and they wouldn't be functioning when JavaScript is off – I would suggest not using the a element at all. From the spec:

If the a element has an href attribute, then it represents a hyperlink (a hypertext anchor). If the a element has no href attribute, then the element represents a placeholder for where a link might otherwise have been placed, if it had been relevant.

I believe that in most cases "action triggers" aren't really fitting the description of the use of an a element. Therefore, I would suggest using a span element, that would be styled in a way that would suggest that it's a trigger for interaction. Quoting the spec again:

The span element doesn't mean anything on its own, but can be useful when used together with the global attributes, e.g. class, lang, or dir. It represents its children.

Another suggestion regarding these triggers: use this line of code (the very first thing after the <title> in the head of your document) in order to give your html element a class js:

<script>document.documentElement.className = 'js';</script>

Then in your CSS do this:

/* 
assuming that this is your mark-up for the pseudo-links:
<span class="action-trigger">Beautiful transition</span>
*/
.action-trigger {
    display:none;
    visibility:hidden;
}
.js .action-trigger {
    display:inline;/* or whatever suits your styling of these elements */
    visibility:visible;
}

This way, when JavaScript is disabled, users won't see "pseudo-links" that would be calling for action, but wouldn't actually work.

UPD. Semantically, in certain cases, you could also use form submit elements, such as <button>: as an example – you may trigger form submission – all the "voting"/"liking"/"deleting" functionality falls into this category. Which, when JS is disabled would really submit that form, but when JS is enabled that would trigger an action on the side of your back-end API.

Bill the Lizard
  • 369,957
  • 201
  • 546
  • 842
Misha Reyzlin
  • 12,998
  • 4
  • 51
  • 62