Skip to content Skip to sidebar Skip to footer

Is There Any Particular Good Reason To Keep Using Href='#' On Javascript Targeted Links?

As you all know, it's extremely common to have code like this: Edit Which is then hooked up to an event handler (using jQuery or whatever'

Solution 1:

in general I don't use a tags for js calls, its sloppy and gives the response you're talking about. If I am doing an anchor totally use it, but if you're not just use a class as your selector and do the rest in css.

Solution 2:

Optimally you would would link to the page that sould be used if the user has Javascript disabled or wants to open the editing screen in a new tab:

<ahref="/edit-profile/"class="edit-profile-link">Edit Profile</a>

That way, if anything goes wrong, no functionality is lost.

Of course, building an AJAX editing screen and a standard editing screen is more work. If you don't want to do all of that and you don't care about Javascript free users you can use this:

<ahref="javascript:;"class="edit-profile-link">Edit Profile</a>

Solution 3:

Using the CSS is better. No, removing the # won't break browser behavior. However, the CSS you've got isn't quite adequate: you'd also have to change the text color and underline style, plus implement hover handling and, optionally, differentiating followed links.

Solution 4:

Setting href="#" in this case is basically wanting to stop the event from it's default behavior. I have actually answered a similar question to this here.

Solution 5:

From a purist perspective I would argue that you are breaking the web by not including the href attribute. If a browser does not support javascript your link will not work for them. That's a purists perspective -- simply pointing an href to "#" doesn't make a difference either way.

In the past you only utilized an anchor without an href to specify a place holder on the page, but it looks like utilizing an anchor tag without an href is being deemed obsolete in HTML5:

http://www.html-5.com/changes/deprecated/a-name-tag.html

So the purist perspective is you break the web.

The realist perspective is if you have to reincorporate some behavior the browser gives you for free in CSS because of your HTML -- that would mean the browser is not happy with something in your HTML. And given that anchors without href's are being deprecated. Common sense tells me to keep using href="#" to avoid a face palm moment when you deal with a weird browser based bug that boils up due to this minor violation of the DOM.

Post a Comment for "Is There Any Particular Good Reason To Keep Using Href='#' On Javascript Targeted Links?"