1

What for using return in such situations?

<div class="s" onclick="return goNav();">click</div>

What is difference?

<div class="s" onclick="goNav();">click</div>

Both variants work

Or:

window.onload = function() {
    var linkWithAlert = document.getElementById("alertLink");
    linkWithAlert.onclick = function() {
        return confirm('Вы уверены?');
    };
};
1
  • 2
    If the goNav function returns false then the default click action will be canceled, which doesn't mean much on a div element but it could on an anchor. Commented Sep 12, 2013 at 17:35

1 Answer 1

2

The return of onclick can be false. In that case, the default onclick action wouldn't be triggered:

<a href="http://stackoverflow.com/" onclick="return confirm('Really?')">Go to SO</a>
<a href="http://stackoverflow.com/" onclick="confirm('Really?')">Go to SO</a>

In the first case, if you click on No, you're not redirected, in the second case, you are. That means that without using return you can trigger your function without breaking the browser`s default.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.