Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use javascript:void(0) instead of # in href? [duplicate]

Tags:

I can see a lot of <a href="javascript:void(0);"> on html pages. From I've read it does nothing by returning undefined. How is this different with <href="#">

like image 546
abiieez Avatar asked Dec 19 '13 08:12

abiieez


People also ask

Why we use JavaScript void 0?

JavaScript void 0 means returning undefined (void) as a primitive value. You might come across the term “JavaScript:void(0)” while going through HTML documents. It is used to prevent any side effects caused while inserting an expression in a web page.

Is JavaScript void 0 Safe?

Generally, you want to avoid href="javascript:void(0)" , as it will cause the browser to parse the value of the link URL, which is both costly and unnecessary. It also introduces a potential XSS security vulnerability, as javascript: URLs violate Content Security Policy (CSP).

Why does it say JavaScript void 0 Chrome?

The main reason why users encounter the said error message is that Javascript is disabled on their browsers. Javascript is a plugin which is mostly disabled by default on all browsers. Here, you have to manually enable the Javascript so the site can render its page using the technology.

How do I bypass JavaScript void 0?

Disable add-ons/extensions If you've suddenly started getting the javascript void 0 error after installing a new extension/add-on, disable it and try again. What is this? Try disabling all extensions/add-ons and then access the website.


1 Answers

<a href="#">link</a> 

adds # to the browser url and jumps to the top of the page.

<a href="javascript:void(0);">link</a> 

simply "ignores" the link click.

<a href="#" onclick="return false;">link</a> 

also ignores the href.

Don't forget that in some cases javascript might be disabled (very uncommon).

like image 180
MaGnetas Avatar answered Sep 20 '22 22:09

MaGnetas