4

I'm using this in my HTML:

 <a href="#" onclick="preload('12345');return false;">Click</a>

It calls the function preload() on an external js-file and works fine so far.

But i have dozens of those links and would like to remove alle those "return false" and put only one directly inside the preload()-function in the js-file.

But it will always be ignored?! Does the "return false" really only work inside the onclick="..."?

abhi
  • 1,760
  • 1
  • 24
  • 40
Chama
  • 189
  • 1
  • 3
  • 11
  • Does this answer your question? [What's the effect of adding 'return false' to a click event listener?](https://stackoverflow.com/questions/128923/whats-the-effect-of-adding-return-false-to-a-click-event-listener) – limido Oct 10 '20 at 20:24

5 Answers5

12
function preload () {
    // some code
    return false;
}

<a href="#" onclick="return preload('12345');">Click</a>

or use addEventListener

For example:

<a href="#" class="link">Click</a>
<script type="text/javascript">
    document.querySelector('.link').addEventListener('click', function (e) {
        // some code;

       e.preventDefault();
    }, false);
</script>
Dmitriy
  • 3,745
  • 16
  • 24
  • 1
    Basically, to explain why it's like this, the contents of the onclick block are a function. Evaluated `false;` is valid (if silly) JavaScript, but basically JavaScript wanted you to actually `return false`. – Compass Mar 03 '16 at 20:19
5

Putting return false; in the inline onclick attribute prevents the default behavior (navigation) from occurring. You can also achieve this by clobbering the onclick attribute in JavaScript (i.e. assigning the .onclick property to be a function that returns false), but that's frowned upon as old-fashioned and potentially harmful (it would overwrite any additional event listeners attached to that event, for example).

The modern way to prevent the <a> element's default click behavior from occurring is simply to call the .preventDefault() method of the triggering event from within the attached event listener. You can attach the listener the standard way, using .addEventListener()

Some examples:

// this works but is not recommended:
document.querySelector(".clobbered").onclick = function() {
  return false;
};

// this doesn't work:
document.querySelector(".attached").addEventListener("click", function() {
  return false;
});

// this is the preferred approach:
document.querySelector(".attachedPreventDefault").addEventListener("click", function(e) {
  e.preventDefault();
});
<a href="/fake" onclick="return false;">If you click me, I don't navigate</a><br/>
<a class="clobbered" href="/fake">If you click me, I don't navigate</a>
<br/>
<a class="attached" href="/fake">If you click me, I navigate</a>
<br/>
<a class="attachedPreventDefault" href="/fake">If you click me, I don't navigate</a>
Thriggle
  • 7,009
  • 2
  • 26
  • 37
0

I think if you put it into the preload function and in the onclick event just put return false will work. Maybe you've tried this code?

<a href="#" onclick="return preload('12345');">Click</a>
vishes_shell
  • 22,409
  • 6
  • 71
  • 81
Ömer
  • 1
  • 1
0

Try to put the return false clause into the inline function:

<input onclick="yourFunction();return false;">
Skatox
  • 4,237
  • 12
  • 42
  • 47
Don Wayne
  • 11
  • 2
-1

I would maybe suggest not using onClick() and instead using similar jQuery.

James Taylor
  • 25
  • 1
  • 7