views:

667

answers:

10
+4  A: 

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
Most places do, but *something* has to go there.
Moshe
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
@SpikETidE: What browser are you using? What does this AJAX function look like?
Andrew Hare
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
Seriously, don't do this. There are all manner of problems, summarised quite well here: http://www.jibbering.com/faq/#javascriptURI
Tim Down
A: 

A similar SO post: Href for Javascript links: “#” or “javascript:void(0)”?

o.k.w
Went through it before posting this question, o.k.w ...But javascript : void(0) is not working in my scenario..
SpikETidE
A: 

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
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
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
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
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
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
You can use window.status to change what the status bar displays as well.
Jeff Rupert
Thanks for the tips, jeff.... Taking a look at it now....
SpikETidE
+2  A: 

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
I am looking for a way to not use the "#" character at all....
SpikETidE
@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
But returning false in the click event won't call the jquery .click() function...
SpikETidE
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
@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
A: 

I always use this:

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

pygorex1
This is the alert i get when i use this in FF 3...Firefox doesn't know how to open this address, because the protocol (javacript) isn't associated with any program.
SpikETidE
but will it work if javascript is disabled?
metal-gear-solid
There's no point in using jquery if the site is intended to work with javascript disabled.....
SpikETidE
SpikEtidE - it's `javascript:;` not `javacript:;` Also, I upvoted nickf's answer, as it is the most elegant solution.
pygorex1
Again, the only solution is a meaningful page URL as a fallback. http://jibbering.com/faq/#javascriptURI
Tim Down
A: 

Give the address of the same page in <base> tag ..(like this way .. <base href="page.html">) no need to give full file path .. and now wherever <a/> tag appears .. keep href="" (blank)..

Being a web (template) designer I always do that for my design templates ..

infant programmer
why would you do that? It just makes it harder to maintain.
nickf
And it takes it to the top of the page each time u click on it.. It's look odd if you want to load content into a particular div located at the bottom of the page....
SpikETidE
And anyway that's what "#" does...
SpikETidE
@Aravind:You could try calling a function during onClick of every link and based on the link that is clicked load the page that is desired...This is to hide the link from showing up in the status bar...
SpikETidE
+24  A: 

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
superb answer .. :)
infant programmer
A: 

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.

pawel
A: 

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 L.
+1  A: 

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;
});
Justin Johnson