Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript: onclick and return false

Tags:

javascript

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="..."?

like image 388
Chama Avatar asked Dec 11 '22 18:12

Chama


1 Answers

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>
like image 144
Dmitriy Avatar answered Dec 22 '22 18:12

Dmitriy