54

The <a> tag is used to create hyperlinks but in this age of jQuery and Ajax we are using it to load HTML into <div>s in the same page as the <a> tags.

That said, we set the href atribute as href="#", using or rather abusing the # character as a placeholder along with some undesirable side effects like the URL getting appended with the # character.

And if you leave the href attribute blank href = "", the link does not seem to work.

Is there anyway to do this in a cleaner way like showing some text or dummy function in the status bar of the browser when the user hovers over the link and yet make the link do what the programmer intended?

Here's my code.

<ul id="sidebarmenu1">
   // List that is converted into a menu... 
   <li> <a href="#" id="loadHotel" > HOTEL </a> </li>
   <li> <a href="#" id="loadCountry"> COUNTRY </a> </li>
   <li> <a href="#" id="loadCity"> CITY </a> </li>
</ul>

// The jQuery to load content into another div with Ajax
var loadUrl = "createHotel.php";
$("#loadHotel").click(function() {
    $("#mainContent").html(ajax_load).load(loadUrl);
}); 

// ajax function to load hotel ---> rooms page 

var url_loadRooms = "viewRooms.php";
$("#createRooms").click(function() {
    $("#mainContent").html(ajax_load).load(url_loadRooms);
});

What else can I use instead of "#" to make my code neat..?

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
SpikETidE
  • 6,711
  • 15
  • 46
  • 62
  • Another thing that can be done is not displaying the actual JS function that will be called when the link is hovered, but show some text in the status bar.... – SpikETidE Dec 09 '09 at 06:18
  • @SpikETidE: Based on your comments to the given answers, I think you have more specific requirements. You probably want to add to your post and specify exactly why/how "void(0)" method doesn't work for you. – o.k.w Dec 09 '09 at 06:28
  • @ o.k.w : Added code for reference... – SpikETidE Dec 09 '09 at 07:09

8 Answers8

45

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.

nickf
  • 537,072
  • 198
  • 649
  • 721
  • 1
    It works, but if you try to validate the document at https://validator.w3.org, you would get this error: "Bad value popup for attribute rel on element a: The string popup is not a registered keyword." – Jaime Montoya May 16 '16 at 04:43
21

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,

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • 1
    Most places do, but *something* has to go there. – Moshe Dec 09 '09 at 06:06
  • Hi Andrew... In my case i call an higher level ajax load function when the user clicks on the a tag. Using href = "javascript:void(0)" nullifies the ajax call and the browser displays "Permission Denied to view this page" .. – SpikETidE Dec 09 '09 at 06:09
  • @SpikETidE: What browser are you using? What does this AJAX function look like? – Andrew Hare Dec 09 '09 at 06:15
  • My code should work alike in all the major browsers... my Ajax functions loads a php page containing the html elements into a div.. this happens when the user clicks on one of the menu items that is already in the page. These menu items are created as an unordered lists whose elements when clicked will call the ajax functions.. Hope i'm clear – SpikETidE Dec 09 '09 at 06:35
  • 4
    Seriously, don't do this. There are all manner of problems, summarised quite well here: http://www.jibbering.com/faq/#javascriptURI – Tim Down Dec 09 '09 at 10:14
  • But this make webamaster error "Access to 'javascript' URIs has been disabled" – Shemeer M Ali Jan 08 '14 at 09:57
  • @TimDown returning false (as the article suggests) is even worse. –  Jan 25 '16 at 14:25
8

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>
Asaph
  • 159,146
  • 25
  • 197
  • 199
  • I am looking for a way to not use the "#" character at all.... – SpikETidE Dec 09 '09 at 06:39
  • @SpikETidE: Put whatever you want in the `href` attribute in my example. It actually doesn't matter because the browser won't follow it. – Asaph Dec 09 '09 at 06:55
  • But returning false in the click event won't call the jquery .click() function... – SpikETidE Dec 09 '09 at 07:03
  • 1
    What about users with browsers with JavaScript turned off? There are plenty of them around. You should put a **meaningful** fallback in the `href` for such users. – Tim Down Dec 09 '09 at 10:16
  • 2
    @Tim Down: The number of *people* actually using the internet with JavaScript disabled is often overstated. The number is actually so small that Google Analytics doesn't even count those people (how can it, it's JavaScript based). Even the major bots these days support JavaScript. In any case, the alternative for the OP (`#`) doesn't exactly represent a meaningful fallback anyway. – Asaph Dec 10 '09 at 05:22
3

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.

Arnis Lapsa
  • 45,880
  • 29
  • 115
  • 195
1

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.

Community
  • 1
  • 1
pawel
  • 35,827
  • 7
  • 56
  • 53
1

nickf beat me to it; however, a few things should be mentioned. You should never use the "javascript" protocol for a link (unless maybe you intend for it to be a bookmarklet). It is the opposite of progressive enhancement. Whenever possible, the href attribute should be an actual URI resource so that it can gracefully degrade. In all other situations, "#" is encouraged and proper JavaScript should be used from preventing the page from scrolling to the top. There are two methods to do so. In the click handler, either prevent the default action, or return false.

$('a[rel*="popup"]').click(function(e) {
    e.preventDefault();
    loadPopup({url: this.href});
});

or

$('a[rel*="popup"]').click(function() {
    loadPopup({url: this.href});
    return false;
});
Community
  • 1
  • 1
Justin Johnson
  • 30,978
  • 7
  • 65
  • 89
0

Using the "#" character as a placeholder basically makes the link "active." The browser interprets it as a tag that points to something else. If href is empty, the browser will assume the a tag is just another tag.

A way around that is to assign some CSS to a different tag that emulates the same functionality of the a tag. On hover, change the mouse, underline, change color, etc. You can easily change the window status and make it seem like the user is clicking on a link when in reality they aren't clicking a link so much as making a click event.

In fact, that's the better option, because running only a JS function through event binding that can't be used without JavaScript shouldn't be considered a link in the first place.

Jeff Rupert
  • 4,690
  • 2
  • 20
  • 17
  • Hi Jeff... As pointed out in this post http://stackoverflow.com/questions/134845/href-for-javascript-links-or-javascriptvoid0 an "a" tag can be accessed by keyboard tab while the other way makes it only a mouse only element... I want mine to be accessed through keyboard also... – SpikETidE Dec 09 '09 at 06:13
  • In that case, I'd look into writing an event binding for a keystroke. Also, I think you can use the taborder attribute in HTML (or something like it) to allow the user to tab to a specific element. Not sure if it works for everything. I've never tried it. What's the benefit of keyboard access in this case? – Jeff Rupert Dec 09 '09 at 06:16
  • Also, you could have the anchor point to any kind of URL but make sure the onclick event handler returns false. This will stop the bubble through and the link will never be accessed. However, if a user has JavaScript disabled, this will give you the ability to have a workaround. – Jeff Rupert Dec 09 '09 at 06:18
  • The keyboard access is just for usability... The link is use is in a menu and it will be desirable if the user can navigate to it using only the tab key... – SpikETidE Dec 09 '09 at 06:19
  • Use tabindex. I think browsers default it with anchors and form elements, but you can add your own options there. http://www.w3.org/TR/html401/interact/forms.html#h-17.11 - That should give you a quick overview of tabindex, and might help your situation. Definitely make sure to use return false; to stop the bubbling, so the link doesn't actually fire, just the click event. – Jeff Rupert Dec 09 '09 at 06:24
  • You can use window.status to change what the status bar displays as well. – Jeff Rupert Dec 09 '09 at 06:26
  • Thanks for the tips, jeff.... Taking a look at it now.... – SpikETidE Dec 09 '09 at 06:40
0

I always use this:

<a href="javascript:;">Click to your heart's delight</a>

leepowers
  • 37,828
  • 23
  • 98
  • 129