Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding "javascript : ... ;" inside <a href=" ">

What is the meanign of javascript:; that is kept inside the href attribute in an link?

Like

<a href="javascript: ... ;">Link</a>
like image 488
pierrotlefou Avatar asked Apr 25 '12 04:04

pierrotlefou


People also ask

Can you put JavaScript in href?

In JavaScript, you can call a function or snippet of JavaScript code through the HREF tag of a link. This can be useful because it means that the given JavaScript code is going to automatically run for someone clicking on the link. HREF refers to the “HREF” attribute within an A LINK tag (hyperlink in HTML).

What does href JavaScript:; mean?

Definition and Usage The href attribute specifies the URL of the page the link goes to. If the href attribute is not present, the <a> tag will not be a hyperlink. Tip: You can use href="#top" or href="#" to link to the top of the current page!

Can we pass function to href?

The anwer is: not possible.

Does Onclick work on link?

This is a type of JavaScript link - the onclick attribute defines a JavaScript action when the 'onclick' event for the link is triggered (i.e. when a user clicks the link) - and there is a URL present itself in the onclick attribute.


1 Answers

IF you need to pass a javascript snippet which needs to run instead of the default behavior of an element then you use this javascript: ; syntax.

For example

<a href="javascript:alert('');">Test</a> <!-- Runs on the click of the link -->

Similarly, you can combine these on other events also, like onclick, onchange etc but this is really not necessary, since you can execute the snippet, directly.

The uses of this, i have seen in years are:

  1. <a href="javascript:void(0);">Test</a>
  2. <form action="javascript:void(0);">..</form>
like image 86
Starx Avatar answered Sep 22 '22 15:09

Starx