Skip to content Skip to sidebar Skip to footer

Alternatives For Using "#" In Href Attribute

The tag is used to create hyperlinks but in this age of jQuery and Ajax we are using it to load HTML into

Solution 1:

The best solution is to not use some dummy placeholder at all. Use a meaningful URL which, if the link were actually followed, would show you the information you'd get from the AJAX request.

I do this regularly with my web apps, using Javascript to enhance a working site. For example, the HTML:

<a href="/users/index" rel="popup">View users</a>

The Javascript (jQuery):

$('a[rel*="popup"]').click(function() {
    loadPopup({    // whatever your favourite lightbox is, etc..
        url: this.href
    });
    return false;
});

The benefits of this are numerous. Your site is accessible to screen readers, web crawlers and users with javascript off, and even those with javascript turned on will get a meaningful URL in their status bar, so they'll know where they're going.


Solution 2:

I usually use this:

href="javascript:void(0);"

Setting an anchor's href attribute to javascript:void(0); indicates to the browser that this anchor is not a hyperlink to another document or anchor,


Solution 3:

If your onclick handler returns false, the link won't get followed by the browser. Try this:

<a href="#" onclick="alert('No # in the address bar!'); return false;">Click Me</a>

EDIT:

If you're absolutely hellbent on not using the octothorpe (ie. # symbol), you don't have to. Try this:

<a href="" onclick="alert('No change in the address bar!'); return false;">Click Me</a>

Solution 4:

Why you need anything to be defined in href?

That's how SO works=>

<a id="close-question-1871874" title="closes/opens question for answering....">
  close<span title="3 more vote(s) needed to close this question"> (2)</span>
</a>

But - if link is supposed to actually navigate somewhere - keep normal href
and just e.preventDefault() regular behavior with jQuery.


Solution 5:


Post a Comment for "Alternatives For Using "#" In Href Attribute"