0

I am thinking about the best solution handling click events avoiding the change of the location in the browser(#).

These are the two scenarios:

1. <a class="someclass" href="#">Click me</a>
   (This is ok, but adds a "#" in the url)  

2.  <a class="someclass" href="javascript:void(0)">Click me</a>
   (Seems to be better, the url never changes, but I don't know about compatibility in all browsers, or even when javascript is disabled)

Which one is the best solution ?

cardeol
  • 2,198
  • 16
  • 25

4 Answers4

0

The href parameter isn't a requirement as far as I know. Just remove it and still hook up the events you need.

Jorge Israel Peña
  • 34,719
  • 16
  • 88
  • 119
0

In first method..Just add return false where you are handling the click event. Then it wont add # to URL

Sankalp Mishra
  • 5,766
  • 4
  • 28
  • 58
0

use Javascript:void(0);.

The void operator was pretty much to only way force the click to do nothing.

Dipesh Parmar
  • 26,601
  • 7
  • 57
  • 86
0

You should use href="javascript:void(0)" in adjacent with onClick because it will prevent the browser from loading a new page.if you want to use onCLick for the hyperlink and require the client to not be direct to a new page after clicking on the hyperlink, return void(0) following href='javascript:void(0)' will prevent that to happen.

Devang Rathod
  • 6,434
  • 2
  • 22
  • 31