How do you cancel your href = "javascript: void (null)"?

I was recently bitten by javascript: void (null); error in my hrefs when applied to images.

In my version of Safari (4.0.3 in Leopard PPC), when I use href javascript: void (null); around the image, the image completely disappears from the layout. Looking around, I see that this is happening in IE as well, although I cannot confirm.

I read using the pound sign (#) with the onclick handler

onclick="return false;"

This works, but I hate the pound attached to the url.

I know that there are various methods. So how do you handle your hrefs?

+3
source share
5 answers

My preferred option

<a href="non_javascript_alternative.html" onclick="doSomething(); return false;">

... ( ). :

  • JavaScript .
+5

<a href="#" onclick="doSomething(); return false;">

<a href="nonJavascriptAlternative.html" id="foo">

javascript - jQuery

$('foo').click(function() { doSomething(); return false; });
+6

false onclick, click , # URL-

<a href="#" onclick="return false;">
+2

URL- hrefs. , ?

 <span onclick="doSomething();" class="fakeLink"><!-- active elements --></span>
+1

Do not use # for the reason you mentioned, and in some browsers, if the user scrolls from the top click on the link, they will return them back. I use this:

<a href="javascript:"><img src=""/></a>

This should not cause problems in any browser (at least I haven't had a problem yet).

+1
source

Source: https://habr.com/ru/post/1721501/


All Articles