Alternatives For Using "#" In Href Attribute
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
});
returnfalse;
});
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:
<ahref="#"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:
<ahref=""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=>
<aid="close-question-1871874"title="closes/opens question for answering....">
close<spantitle="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:
Maybe I don't get smething, but if there is no link, you just shouldn't use an <a /> element in the first place. Use some <span /> or attach event listeners to list elements. You can style these elements to have cursor: pointer;
using CSS.
Remember that browsers have some special actions associated with links, like "open in new tab", "save target element" etc. When you use dummy href=''
attribute these actions work in a broken way, so it's better to not use links at all.
On the other hand, if you are able to render content of these ajaxified parts as normal pages (and it makes sense), follow nickf's advice.
Post a Comment for "Alternatives For Using "#" In Href Attribute"