April 25, 2012

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

Question by pierr

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

Like

<a href="javascript: ... ;">Link</a>

Answer by Starx

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>

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!