Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onclick="javascript:func()" vs. onclick="func()" [duplicate]

Tags:

javascript

Is there any difference between

<input type="submit" value="Register" onclick="javascript:submitTheForm();">

and

<input type="submit" value="Register" onclick="submitTheForm();">

Should I use javascript: before a JS function call?

like image 221
Pat Wangrungarun Avatar asked Feb 03 '23 05:02

Pat Wangrungarun


2 Answers

javascript: inside an onwhatever="" handler is a no-op. The content of this attribute is always JavaScript so javascript: defines a label - but outside of a switch block or a loop (where you can use it to break/continue to the position of the label) it does nothing.

However, using inline event handlers is discouraged, there are better ways to register event handlers.


While not mentioned in the question, it's worth noting that using javascript: in the href attribute does work (and in there it is actually necessary) but is highly discouraged for various reasons:

  • this does not point to the element
  • Calling a function returning a value causes the browser to leave the site
  • Clicking the link with disabled JavaScript breaks
  • Trying to open the link in a new window/tab breaks

So, if you really have to use inline events, always use onclick="" etc. without javascript:.

like image 127
ThiefMaster Avatar answered Feb 04 '23 19:02

ThiefMaster


When you are in a onclick attribute, you shouldn't use the javascript: prefix. This is usefull when you want a link <a /> to handle a JS function, for example:

<a href="javascript:myfunc();"></a>
like image 29
sp00m Avatar answered Feb 04 '23 18:02

sp00m