Is there a reason why I should call a JavaScript method as follows?
onClick="Javascript:MyMethod();"
Or can I just call it like this:
onClick="MyMethod();"
Is there any difference?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The value of an
onclickattribute is code, not a URI, so this is correct (though not the only way you might do it, see tangent #1 below):This is incorrect but largely harmless:
Sometimes people think the latter is using the
javascriptprotocol, like on links, but it isn’t. It’s doing something else entirely. The language of the code in theonclickattribute is defined at page level (and defaults to JavaScript), and so what you’re actually doing is declaring a label in your JavaScript code, and then callingMyMethod. JavaScript has labels (see tangent #2 below), though they’re not used much.The
onclickattribute is totally different from thehrefattribute on links:There, since we’re putting code where a URI is expected, we have to specify a URI using the
javascriptprotocol, so the browser knows what we’re doing.javascriptis a protocol (likehttpormailto) that Brendan Eich (creator of JavaScript) was clever enough to define and register (and implement) very, very early on so it’s well-supported.Finally: Best to make
onclickall lower case, not mixed case, although it only really matters if you use XHTML.Tangent #1
Perhaps a bit off-topic, but: Using the HTML attributes for hooking up handlers is perfectly valid and works well cross-browser, but it intermixes your JavaScript event hookup with your HTML. Some people see that as a good thing, others belong to the “unobtrusive JavaScript” side and think you should hook everything up later. What you do is up to you. The unobtrusive approach is particularly useful when your HTML designers and your JavaScript coders are not the same people (as happens frequently on large teams).
The unobtrusive approach basically says: Don’t use the HTML attributes for this, do it later from script. So instead of
you might have this HTML:
combined with this JavaScript:
…where
setTabuses context to figure out which tab was clicked and act accordingly, andhookUpTabsis called as soon as the DOM is ready. Note that where we’re setting up the click handler, we’re assigning a function reference, not a string, to theonclickon the tab div.I wouldn’t actually use
onclickin the above, I’d use DOM2 handlers via theaddEventListener(standard) /attachEvent(Microsoft) functions. But I didn’t want to get into standard vs. Microsoft stuff. And you don’t either, if you start doing unobtrusive JavaScript, use a library to handle that stuff for you (jQuery, Prototype, Closure, whatever).Tangent #2
Another mildly off-topic tangent: So, what are these JavaScript labels then? Details in the spec as always, but here’s an example of using labels with a directed
breakstatement in a loop:Live Example