rjmcb
rjmcb

Reputation: 3745

javascript:void(0) too long (shorter way)

I know this is a dumb question but is there a shorter way of writing

javascript:void(0) for anchor links?

example :

<a href="javascript:void(0)" onClick="functionHere()"> Click me </a>

I don't like to use "#" because clicking the link brings me to the top of the page

Upvotes: 3

Views: 1468

Answers (5)

Rafael Herscovici
Rafael Herscovici

Reputation: 17124

Even due there is an answer allready selected, i wanted to contribute my opinion on this.

using an href with a javascript:; javascript:void(0); javascript:return false; is bad practice search engine's will try to crawl your href and will find a broken link.

that being said, i do understand that sometimes you do need a link that follows nowhere, but executes something on the page.

i would have done it this way:

<script>
var elm = document.getElementById('myElement'); 
elm.style.cursor = 'pointer'; 
elm.onclick = function() { 
    // do something... 
}; 

</script>

<span id="myElement">Click Here</span>

this way, you html code stays clean and nice. please dont look at this as "Good Coding", since you allways need to keep a version for browsers with javascript disabled (yes, yes, i know that 99% will have it enabled ), But if evil must be done, may it be the less evil possible at least.

Upvotes: 4

Kernel James
Kernel James

Reputation: 4074

function a() {};

<a href="javascript:a();">
<a href="javascript://">
<a href="don't_load" onclick="doit();return false;">

Upvotes: -2

Abhidev
Abhidev

Reputation: 7273

Try this

<a href="#" onClick="return false;"> Click me </a>

Upvotes: 2

midishero
midishero

Reputation: 107

Another way to do that:

<style type="text/css">
.pointer{
  cursor:pointer;
}
</style>
<a class="pointer" onclick="functionHere()"> Click me </a>

Upvotes: 2

user1150525
user1150525

Reputation:

A shorter version is:

<a href="javascript:;" ...>

Upvotes: 2

Related Questions