Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why we are using javascript:void(0) in Angular code [duplicate]

  <a href="javascript:void(0)" id="loginlink">login</a>

I've seen such anchor tag's href property having javascript:void(0) value many times. I don't know what exactly that means.I saw this when working with angular 5. Why is javascript:void(0) used as a value for href property?

like image 559
Guvaliour Avatar asked Feb 14 '18 03:02

Guvaliour


People also ask

Why do 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).

How does JavaScript void work?

If inserting an expression into a web page results in an unwanted effect, then use JavaScript void to remove it. Adding “javaScript:void(0)”, returns the undefined primitive value. The void operator is used to evaluate the given expression. After that, it returns undefined.


1 Answers

javascript:void(0)

The snippet show above simply ignores the link "click." This can be done in a similar fashion by the following:

<a href="#" onclick="return false;">link</a>
like image 180
Rob Avatar answered Sep 22 '22 03:09

Rob