tags:

views:

116

answers:

8

Which is preferable, assuming we don't care about people who don't have JavaScript enabled?

<a href="#" onclick="executeSomething(); return false"></a>

Or

<a href="javascript:executeSomething()"></a>

Is there any difference?

Or there any other ways I'm missing besides attaching an event to the anchor element with a JavaScript library?

+4  A: 

it's better to use the onclick because that's the way the things should be.

The javascript: was somekind of hackish way to simulate the onclick.

Also I advice you to do some non intrusive Javascript as much as possible, it make the code more easy to read and more maintainable!

RageZ
+3  A: 

href="#" has a number of bad side effects such as showing # in the browser footer as the destination URL, and if the user has javascript disabled it will add a # at the end of their URL when they click the link.

The best method IMHO is to attach the handler to the link in your code, and not in the HTML.

var e = document.getElementById("#myLink");
e.onclick = executeSomething;
joshperry
A: 

This one works for me.

<a href="Javascript:void(0);" onclick="executeSomething(); return false"></a>
Kieran
The # will not redirect / refresh the page as long as you use return false;
Doug Neiner
Basically IE can have problems with the javascript: statement and the pound symbol is a much better alternative.http://blog.reindel.com/2006/08/11/a-hrefjavascriptvoid0-avoid-the-void/http://www.ehow.com/how_5317432_avoid-javascript-void.html
Ambrosia
About the #. The # is just an empty anchor, since it is empty, the browser don't find a suitable one and returns to the top. It doesn't reload the page since this is not the behavior of anchors.
Mike Gleason jr Couturier
+2  A: 

Best practice would be to completely separate your javascript from your mark up. Here's an example using jQuery.

 <script type="text/javascript">
     $(function() {
         $('a#someLink').click( function() {
              doSomething();
              return false;
         });
     });
 </script>

 ...

 <a href="#" id="someLink">some text</a>
tvanfosson
There is no mention of jQuery anywhere in the post, I think it might be confusing for the guy who asked the question... my 2 cents
Mike Gleason jr Couturier
Not sure why that's best practice, seems like a maintenance nightmare to search .js files trying to figure out what's handling all the links. Plus that's an awful lot of code to replace `onclick="myFunc()"`
Rob
@Mike -- I just wanted a quick example of how to apply the function in javascript. Obviously any framework or raw javascript would work as well.
tvanfosson
@Rob - because it keeps your "code" together rather than spread throughout the markup. And, yes, it is a bit more code, but using different selectors the same function can be applied to many different elements. If, for instance, I wanted to execute a particular function on all link elements I'd simply have to change the selector and, perhaps, change the function to take the link as a parameter. It's even more apparent when the level of complexity goes up.
tvanfosson
@Rob, good point for small projects, but I also share tvanfosson's point of view. Also, the snippet doesn't have to be in a library. I often attach all my handlers in the bottom of the page, where other variables are declared and ready to be used when events are occuring... thus keeping all javascript in the same place!
Mike Gleason jr Couturier
@tvanfosson Yes I fugured that out before posting but still :) Just a mention that it's jQuery could help if he tries your example without knowing it's a third party library! And it's the first time I see `$( <function> )`, did you meant the "ready" approach or the "declare function and call" approach? Thanks!
Mike Gleason jr Couturier
@Mike -- I did mention it was a jQuery example.
tvanfosson
+4  A: 

The nice thing about onclick is you can have the link gracefully handle browsers with javascript disabled.

For example, the photo link below will work whether or not javascript is enabled in the browser:

<a href="http://www.example.com/myPhoto.jpg" target="_blank" onclick="showPhoto(this.href); return false;">foobar</a>
Rob
Downvoted for caring about people with JavaScript disabled.
JamesBrownIsDead
A: 

Yes I would agree to use onclick and leave the href completely out of the anchor tag... Don't know which you prefer to do but I like to keep the 'return false' statement inside by function as well.

Ambrosia
The last I tried if you leave the href attribute out the element isn't treated as a hyperlink by most browsers. Not sure if this is still the case today as it's been awhile since i've tried it...
Rob
A: 

The main difference is that:

  1. The browser assume by default the href attribute is a string (target url)
  2. The browser knows that in a onclick attribute this is some javascript

That's why some guys specify to the browser "hey, interpret javascript when you read the href attribute for this hyperlink" by doing <a href="javascript: ... ;">...</a>

To answer the question, that's the difference!

OTOH what's the best practice when using javascript events is another story, but most of the points have been made by others here!

Thanks

Mike Gleason jr Couturier
+1  A: 

This is essentially the pattern you'd want to follow:

  • Write your HTML markup
  • Attach event handlers from JavaScript

This is one way:

<a id="link1" href="#">Something</a>

<script type="text/javascript">
// get a reference to the A element
var link1 = document.getElementById("link1");

// attach event
link1.onclick = function(e) { return myHandler(e); };

// your handler
function myHandler(e) {
    // do whatever

    // prevent execution of the a href
    return false;
}
</script>

Others have mentioned jQuery, which is much more robust and cross-browser compatible.

Jeff Meatball Yang