1

Is there any benefit of using

<asp:ImageButton ID="btn" runat="server" OnClientClick="javascript:return validate();" />

instead of

<asp:ImageButton ID="btn" runat="server" OnClientClick="return validate();" />

?

What is the purpose of prepending with javascript:? Should I use it? Pros and cons?

Atomic Star
  • 5,427
  • 4
  • 39
  • 48
  • 2
    You shouldn't be coding obtrusively (aka inline javascript). – Johan Oct 01 '13 at 10:07
  • 1
    @Johan, calling a method from onclick is fine, it's not really inline javascript. – Ash Burlaczenko Oct 01 '13 at 10:11
  • @AshBurlaczenko fair enough, I just believe to separate the JS from the HTML – Johan Oct 01 '13 at 10:12
  • @AmitAgrawal that question has a different context – Atomic Star Oct 01 '13 at 10:19
  • @Johan how would you do it without inline javascript? – Atomic Star Oct 01 '13 at 10:20
  • @EwaldStieger there are multiple ways,let me create a jsfiddle, will post it here, when I am done – Johan Oct 01 '13 at 10:23
  • 1
    @EwaldStieger, have a look at this, two simple examples http://jsfiddle.net/Johan_fiddle/SU7Nb/2/. I will add jquery examples too, which makes your life even easier. :) – Johan Oct 01 '13 at 10:26
  • 1
    @Johan Ok, that is easy. Will do it that way from now on :) – Atomic Star Oct 01 '13 at 10:40
  • @AshBurlaczenko If that's not inline JS, what is then? – JJJ Oct 01 '13 at 12:44
  • @Juhana, I should of emphasised the really, to me inline javascript would be anything other that method calls. The point a was making is that IMO methods with onclicks are fine as they improve readability and make it easier to debug. Either way this is down the the developer to decide what they prefer. – Ash Burlaczenko Oct 01 '13 at 13:39
  • @AshBurlaczenko That's fair, but you can't say that it's not inline JavaScript because that's what it objectively is. You can say that it's ok to use it but that doesn't change what it is. It's like saying "I don't think 3 is a number because I don't like 3." – JJJ Oct 01 '13 at 13:49

3 Answers3

2

It is a label. Since there is no loop (to break or continue from), it is also a useless waste of bytes.

People who include it are cargo cultists who copy/pasted it from people who copy pasted it from href attributes (where it serves to states that the content comes from executing javascript rather then fetching over HTTP or another protocol).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

You shouldn't use it. javascript: is a label and should only be used if you were to put it into a location or href.

OnClientClick executes javascript anyway, so it is redundant here.

CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
-4

the old browser doesn't know which language you use without the "javascript" tag.

I would use the javascript tag for following reasons:

  • Everybody who reads your code, know that you use javascript there
  • better compatibility with older browsers
  • better reading for developers
Getu.ch
  • 94
  • 5
  • 4
    Every browser that has ever supported `onclick` has assumed JavaScript if nothing contradicted it. HTML provides no way to specify the language on a per-intrinsic-event-attribute basis, only [globally for the document](http://www.w3.org/TR/html4/interact/scripts.html#h-18.2.2). – Quentin Oct 01 '13 at 10:08