0

Is there any difference between

<a href="javascript:void(0)">Link</a>
<asp:Button ID="btn" runat="server" OnClientClick="return Function()" OnClick="btn_Click" Text="Button" />

and

<a href="javascript:void(0);">Link</a>
<asp:Button ID="btn" runat="server" OnClientClick="return Function();" OnClick="btn_Click" Text="Button" />

?

(The ; after the JavaScript is the difference - does this have any effect on any specific browsers or anything?)

user982119
  • 2,455
  • 8
  • 45
  • 71
  • 2
    Belongs to [Code Review](http://codereview.stackexchange.com). – Jeff Noel Jul 15 '13 at 17:44
  • 2
    You should avoid words like "best" in your title. Some people will jump the gun and close the question as "opinion based". The question I think is fine but the title makes it seem off topic. – James Montagne Jul 15 '13 at 17:45
  • 3
    In your simple examples, there's no difference. It can become "necessary" in some/several scenarios though. Here's some info on semicolons: http://stackoverflow.com/questions/444080/do-you-recommend-using-semicolons-after-every-statement-in-javascript ||| http://mislav.uniqpath.com/2010/05/semicolons/ – Ian Jul 15 '13 at 17:46
  • both void(0) and "return Function()" do absolutely nothing! a semi is the least of the concerns here... – dandavis Jul 15 '13 at 17:46

2 Answers2

6

JavaScript has automatic semicolon insertion, so there is no significant difference when you're simply setting an inline event handler with a one-liner.


That said, you shouldn't be adding your event handlers to inline attributes. Instead, you should keep your HTML in .html files, your CSS in .css files and your JS in .js files. Bind events using addEventListener (or jQuery's on method), and you'll be able to change your bound events in one place, rather than having to search through every HTML file where you might have added an [onclick] attribute.

zzzzBov
  • 157,699
  • 47
  • 307
  • 349
4

No, the ; is if you want to execute multiple commands one after another.

Example:

onclick="alert('hello world'); alert('hello world2')"

JSFiddle: http://jsfiddle.net/menelaosbgr/UggHq/


Edit:

As zzzzBov answered, javascript has automatic semicolon insertion.

What are the rules for JavaScript's automatic semicolon insertion (ASI)?

Community
  • 1
  • 1
Menelaos
  • 20,773
  • 14
  • 71
  • 130