views:

163

answers:

3

I have a sample application which has something like below.

<a href="javascript:{}" id="anchorlnk">Go</a>

which javascript function does the link call?

+1  A: 

Simple: nothing.

Andrew
+6  A: 

It does nothing, the {} on the link creates a new empty object that is unused, is not a function call...

Maybe your events are binded on the page load, and someone put this just to do nothing. You could use simply href="#"

CMS
+2  A: 

People use different techniques to do nothing :) More examples are:

href="javascript:;"
href="javascript://"
href="javascript:void();"

If you use

href="#"

then you probably will have an onclick event handler, make sure you return false at the end of your "onclick code", otherwise the page will be scrolled to the top which can be very unpleasent to the user.

presario
href="#" (with ‘return false’) is still the better method to use, however, if you must have a JavaScript link with no HTML backup. ‘javascript:’ URLs are utterly evil and must be purged! :-)
bobince
I know they are unelegant, but I still don't get why exactly are they "utterly evil"! :)
Danita
I guess they are not so bad when you don't want failover in case JS fails. For example, when you have added the link using JS in the first place.
Rakesh Pai
Open-in-new-tab/window and the various other middle/right-click functions tend to go badly wrong with javascript: URLs. But JS URLs are “evil” on a theory level because they break the concept of URLs being the Location of something. This has caused enormous and ongoing browser security problems.
bobince