eBay MIND Patterns
  • Introduction
  • Messaging
    • Alert Dialog
    • Confirm Dialog
    • File Preview Card
    • Form Validation
    • Inline Notice
    • Input Meter
    • Input Validation
    • Page Notice
    • Star Rating (static)
    • Time
    • Toast Dialog
    • Tourtip
  • Input
    • Button
    • Checkbox
    • Chips Combobox
    • Combobox
    • Date Picker
    • File Input
    • Input Dialog
    • Listbox
    • Listbox Button
    • Menu
    • Menu Button
    • Phone Input
    • Radio
    • Select
    • Star Rating (interactive)
    • Switch
    • Toggle Button
    • Toggle Button Group
  • Navigation
    • Breadcrumbs
    • Fake Menu Button
    • Fake Tabs
    • Link
    • Pagination
    • Skip Navigation
    • Tile
  • Disclosure
    • Accordion
    • Carousel
    • Lightbox Dialog
    • Details
    • Flyout
    • Footnote
    • Infotip Button
    • Panel Dialog
    • Pulldown List
    • Segmented Buttons
    • Tabs
    • Tooltip
  • Structure
    • Description List
    • Form
    • Heading
    • Image
    • Item Tile
    • Layout Grid
    • Region
    • Table
    • Table Cell
  • Techniques
    • Active Descendant
    • Ambiguous Label
    • Background Icon
    • Keyboard Trap
    • Live Region
    • Offscreen Text
    • Roving Tabindex
    • Skip to Main Content
    • Alternative Text
  • Anti-Patterns
    • Disabling Pinch-to-Zoom
    • Hand Cursor on Buttons
    • JavaScript HREF
    • Layout Table
    • Mouse Hover on Static Elements
    • Open New Window
    • Setting Focus on Page Load
    • Tabindex-itis
    • Title Tooltip
  • Appendix
    • ARIA Essentials
    • Checklist
    • FAQ
    • Keyboard Interface
    • Known Issues
    • Legacy Patterns
      • Fullscreen Dialog
    • MIND Pattern Template
    • Pattern Naming Scheme
    • References
    • Utilities
Powered by GitBook
On this page
  • Bad
  • Good
  • Even better
  1. Anti-Patterns

JavaScript HREF

You probably need a button, not a link.

Never use "javascript:" "#" or similar as the value of an href attribute.

The purpose of the href attribute is to specify a URL only. If you wish to run JavaScript on an element click, use a button instead.

Bad

<a href="javascript:;" id="share">Share on Facebook</a>

<script>
    $('#share').on('click', function(e) {
        // do fb share stuff
    });
</script>

Good

<!-- a button created with JavaScript -->
<button id="share">Share on Facebook</button>

<script>
    $('#share').on('click', function(e) {
        // do fb share stuff
    });
</script>

Even better

Use progressive enhancement:

<a href="http://www.facebook.com/share?id=12345" id="share">Share on Facebook</a>

<script>
    $('#share').attr('role', 'button');
    $('#share[role=button]').on('click', function(e) {
        this.preventDefault();
        // do fb share stuff
    })
</script>

The screen reader will announce "Share on Facebook. Button" or fallback to "Share on Facebook. Link" if JavaScript is not ready or available.

PreviousHand Cursor on ButtonsNextLayout Table

Last updated 2 years ago