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,218
  • 17
  • 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
  • 36,800
  • 16
  • 93
  • 123
0

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

Engineer
  • 5,911
  • 4
  • 31
  • 58
0

use Javascript:void(0);.

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

Dipesh Parmar
  • 27,090
  • 8
  • 61
  • 90
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,650
  • 2
  • 23
  • 32