against

Syntax difference in javascript function call?

What's the difference between:

<div onclick="return function()"></div>

against

<div onclick="function()"></div> 

They seem to be doing the same for me, and I'm not sure which one to use.

+5
source share
3 answers

Take, for example, a <form onsubmit="return validate();">...feed will be canceled if validate () returns false.

C The <form onsubmit="validate();">...view will continue regardless of the return value of validate ().

+10
source

Explanation in words.

One element returns the value of the element whose attribute is located, the other will not.

return function (), click , function () , true.

, click onclick.


onclick="function ()" click , , .


, .

function some_func () { return false; }

<a href="http://google.com" onclick="return some_func()">
  link #1
</a> <!-- user will never end up at google -->

javascript , .

<a href="http://google.com" onclick="some_func()">
  link #1
</a> <!-- user will always end up at google -->

+2

, return, , true false, , return is true. .

+1

All Articles