Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Difference between onclick and href="javascript:function name?

Tags:

javascript

Is there any difference between

1 : <a href="javascript:MyFunction()">Link1</a>

and

2 : <a href="#" onclick="MyFunction()">Link2</a>

? Would one affect the page performance by any means ?

like image 898
Shyju Avatar asked Apr 05 '10 15:04

Shyju


2 Answers

If your element is not actually supposed to link the user someplace, don't make it an anchor element. If you're using <a> tags just to get the underline/cursor change - don't. Use CSS on a <span> (or other element) instead.

span.link {
  text-decoration: underline;
  color: blue;
  cursor: pointer;
}

Keep your HTML semantic and use anchor elements only when you want to link the user somewhere.

like image 65
Peter Bailey Avatar answered Oct 20 '22 23:10

Peter Bailey


No performance difference.

The first is crap because it will fail completely for users without JS enabled.

The second is still crap, but would be better if the href pointed to a URL for users without JS enabled.

like image 39
Amy B Avatar answered Oct 20 '22 22:10

Amy B