When defining a JavaScript call, like so:
<input type="button" onclick="someJavaScriptFunction" />
What is the best way to write the actual call? I’ve seen numerous method:
javascript:someJavascriptFunction();
or
justSomeJavaScriptFunction();
or
withoutTheSemicolon()
Are there any benefits or caveats to any of the methods? I realize that using addEventListener is more flexible in certain cases.
If you want best practice, separate markup from script content. Assign your event handlers in
scriptelements. As you say, usingaddEventListeneris one way of doing this, although you have to useattachEventas well if you want to support versions of Internet Explorer before version 9.It’s perfectly valid, however, to assign
oneventproperties. For instance:This may well suit your purpose adequately, if you only have a few items.