Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent reload with onclick without "#"?

Tags:

I wish to put some instructions with a link - onclick calling a script that display a simple alert box. If I did like this...

<label for="arquivo">Máximo de 1MB, observe os <a href="" onclick="ajudaUpload();">tipos permitidos</a>.</label>

the page is reloaded even with a return false, and if I did like this...

<label for="arquivo">Máximo de 1MB, observe os <a href="#" onclick="ajudaUpload();">tipos permitidos</a>.</label>

with the "#" symbol, the page is scrolled to the top and "#" is added to query string. Is there a third way to do it without reloading, scrolling and garbage?

like image 277
Gustavo Avatar asked Jul 16 '13 15:07

Gustavo


People also ask

How do I stop my page from reloading on button click?

If you want to avoid full page refresh, then don't use UpdatePanel control as it will only give more pain the butt. Instead, use AJAX with client-side grid (e.g jQuery grid, Webgrid, etc) and handle everything at the client (JavaScript code).

How do I stop href from reloading page?

If you put # inside the href like <a href="#"></a> then the link will not refresh or reload when clicked.

How do I stop the button from refreshing the page react?

Use the preventDefault() method on the event object to prevent a page refresh on form submit in React, e.g. event. preventDefault() . The preventDefault method prevents the browser from issuing the default action which in the case of a form submission is to refresh the page.


2 Answers

Return false after the call:

<a href="" onclick="ajudaUpload();return false;">tipos permitidos</a>

Or if your function returns false then you can return the result of the function:

<a href="" onclick="return ajudaUpload();">tipos permitidos</a>

It's not enough to just return false in the function, you need to actually return false from the click handler.

like image 181
MrCode Avatar answered Sep 20 '22 13:09

MrCode


You can use .preventDefault() method, or return false, or remove the HREF tag all together. Either should work just fine.

Vc nao deviar estar usando onclick dessa forma pra comecar. Ja eh bem antigo e nao se usa assim mais.

like image 36
Jonny Sooter Avatar answered Sep 21 '22 13:09

Jonny Sooter