html - javascript:void(0) too long (shorter way) - Stack Overflow

I know this is a dumb question but is there a shorter way of writing javascript:void(0) for anchor lin

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

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

Share edited Mar 20, 2012 at 12:14 Lightness Races in Orbit 386k77 gold badges666 silver badges1.1k bronze badges asked Mar 7, 2012 at 6:41 rjmcbrjmcb 3,7459 gold badges34 silver badges47 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 4

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.

A shorter version is:

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

Another way to do that:

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

Try this

<a href="#" onClick="return false;"> Click me </a>
function a() {};

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744911263a4600564.html

相关推荐

  • html - javascript:void(0) too long (shorter way) - Stack Overflow

    I know this is a dumb question but is there a shorter way of writing javascript:void(0) for anchor lin

    1天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信