What is the difference between the following?
When do I use the
Can I call the function without using
Please help me understand the difference.
In an element's inline event handler, like
onsubmit, etc., you do not need the
href syntax that I explain below. It doesn't cause an error - I believe it is interpreted as a label - but it is unnecessary.
It doesn't matter whether you want to call a function or run a "simple" JS statement, either way don't include
onclick="doSomething('some val');" onclick="return false;" onclick="doSomething(); doSomethingElse(); return false;"
If you are using inline event attributes don't use the
(I say "if you are using inline event attributes" because this practice is really outdated: it is better to assign the event handlers using JS in a script block.)
You only need the
href attribute like this:
That is because the browser normally expects the
href to contain a URI, so the
href that directs the user to a page telling them to enable JS, and include an
onclick to do the JS functionality:
<a href="/enableJS.html" onclick="doSomething(); return false;">Whatever</a>
That way the link does something useful for users whether they have JS enabled or not. The
return false at the end of the click handler prevents the default behaviour for a click (which would be to navigate to the specified URL).