Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prefered way to display anchors that don't need an href?

What is the preferred way to display an anchor that doesn't need an href attribute (because it's only used in javascript)?

For example, <a href="#">example 1</a> vs. <a href="javascript:;">example 2</a>.

like image 814
Sam Avatar asked Jun 16 '10 09:06

Sam


People also ask

Can I have an anchor without href?

Yes, it is valid to use the anchor tag without a href attribute. 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, consisting of just the element's contents.

What can I use instead of an anchor tag?

If you chose not to use anchor links for clickable functional elements, then: Use cursor: pointer; so users get the cursor that looks like you can click like they are used to. Use the tabindex attribute on the element so keyboard users can tab to it. Define :hover , :active , and :focus styles.

Do anchor tags require the href attribute?

The anchor tag has a required attribute, href (short for “Hyperlink Reference”), and the value of this attribute is a URL which represents the destination for the browser when the link is clicked. The anchor tag also supports a special option attribute, target.

How do I make an anchor tag non clickable?

How do I make an anchor tag non clickable? In order to disable a HTML Anchor Link (HyperLink), the value of its HREF attribute is copied to the REL attribute and the value of HREF attribute is set to an empty JavaScript function. This makes the HTML Anchor Link (HyperLink) disabled i.e. non-clickable.


3 Answers

You should always set a fallback link for users without Javascript. When calling the javascript, you can return false from the handler to avoid the link activating. For example:

<a href="page.html" onclick="doSomething(); return false;">example 1</a>

The alternative, if there is no appropriate page to link to, is to not use a link at all. Use a button instead. You could style it to look like your links if that's really necessary.

like image 140
DisgruntledGoat Avatar answered Oct 20 '22 21:10

DisgruntledGoat


Ideally the href should do something as close as possible to what the javascript was going to do.

For example, if the Javascript was going to pop-up a lightbox with a larger picture, the href should take you to a new page where you can see the same bigger picture.

If you take this approach, people without javascript still get a good experience and people with javascript get an smoother, glossier experience.

Check out Unobtrusive JavaScript and Progressive enhancement

like image 24
Rik Heywood Avatar answered Oct 20 '22 21:10

Rik Heywood


As mentioned by other users, if a non javascript fallback is possible than use that as your href.

Example of opening an image in a popup for users with javascript, while falling back to a regular page load for users without (using jQuery):

<a class="popup" href="path/to/image.png">Baby Llama Picture</a>

<script>
   // Assuming a function called popUpImage does the magic

   $('.popup').click(function(event) {
      popUpImage($(this).attr('href'));
      event.preventDefault();
   });
</script>

Any links that have no useful defined behaviour without javascript should be injected into the page using javascript. This way users without javascript will never see them, and there is no need to provide a valid href.

Example of inserting javascript only links dynamically (using jQuery):

<img class="editable" src="baby-llama.png" />

<script>
  // Assuming a function doEditing that allows javascript based editing of an image

  $(function() {
    $('.editable').each(function() {
      var editable = $(this);

      var editLink = $('<a href="">Edit image</a>');
      editLink.click(function() {
        doEditing(editable);
      });
      editable.insertAfter(editLink);
    });
  });
</script>
like image 2
Karl Avatar answered Oct 20 '22 22:10

Karl