6

I have a href which pointed to #

<a href="#" id="bla" >Bla<a>

I have onclick function which displaying popup on click on that a href.

function doingClick()
{
   //display popup
   return false;
}

But after click symbol # every time added to the url in browser.

So for example if url was like that before I click on my link http://mywebsite.com

But after click on a href the url looking like that: http://mywebsite.com#

Is there any way to avoid such behavior?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Joper
  • 8,019
  • 21
  • 53
  • 80

10 Answers10

16

To avoid this try adding return false;

<a href="#" onclick="doingClick(); return false;">Link</a>

You could also use void(0)

<a href="javascript:void(0)" onclick="doingClick();">Link</a>

There's a popular question related to this (small religious war!) at Which "href" value should I use for JavaScript links, "#" or "javascript:void(0)"?

Community
  • 1
  • 1
Peter Kelly
  • 14,253
  • 6
  • 54
  • 63
  • This is my preferred option. I almost feel guilty not telling the user that the link they're about to click uses Javascript, so I often use `javascript:void(0)` - having said that, you might want to put a real link in the `href` and change it to `javascript:void(0)` using Javascript, so that users without JS still go somewhere. Just my 0.02 – Nils Luxton Aug 18 '11 at 08:20
4

While there are other valid solutions, I personally prefer this shorter solution.

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

Another benefit is that this solution does not scroll to the top of the window.

Oskar Sjöberg
  • 2,728
  • 27
  • 31
4
<a href="#" id="bla" onclick="return doingClick()">Link</a>

Do not forget to return the return of your function. Otherwise you will just call it without suspending the subsequent events.

Rolice
  • 3,063
  • 2
  • 24
  • 32
3

So i think the better way of doing this is to remove href from a element

<a id="bla" class="href" >Bla</a>

and than to make it looks like a href just add simple css class

.href 
{
    color: #2289b8; //color of the link
    cursor: pointer;
}

This idea comes to me when i looked in to source of SO add comment button

Joper
  • 8,019
  • 21
  • 53
  • 80
2

Instead of adding a href, you could add a style="cursor:pointer;" this has the same effect of displaying it like a hyperlink, without the in-page anchor effect.

<a id="bla" onclick="return doingClick()" style="cursor:pointer;">Link</a>
Joachim VR
  • 2,320
  • 1
  • 15
  • 24
1

The url is not pointed to nowhere. The URL is a relative URL to # in other words the URL resolves to <current_url># which in this case is http://mywebsite.com#.

To avoid this behaviour, you have to change the URL.

If you have a onclick-handler that returns false, then that should prevent the link being active :

<a href="#" onclick="return doingClick();">link</a>

You can also use javascript:void(0) as the link href.

In either case, be mindful of the decreased accessibility of your site when you use javascript to access some parts of it. Those users that have javascript disabled, doesn't have javascript enabled browsers or use a screenreader or other accessibilty tools may not be able to use the site.

Aleksi Yrttiaho
  • 8,266
  • 29
  • 36
1

If you don't get better answer, you could make nasty ugly workaround by placing script tag very early on page (on the beginning of body or in head) with following javascript:

if(document.location.href[document.location.href.length-1]=='#'){
    document.location.href = document.location.href.substring(0, document.location.href.length-2)
}

I DO NOT RECOMMEND you to do this as will cause double requests to server when # is in url, but it is here if you have to.

Goran Obradovic
  • 8,951
  • 9
  • 50
  • 79
0

The # is used for linking the element ID's, and will always be added to your URL when used in "empty" hrefs. What you could do, if it REALLY annoys you, is to remove it from location.href in your doingClick function

Thor Jacobsen
  • 8,621
  • 2
  • 27
  • 26
0

A simple workaround would be set the href empty:

<a href="" id="bla" onClick="alert('Hi');">Bla<a>

Which still works.

Devator
  • 3,686
  • 4
  • 33
  • 52
0

If you want to keep the "events" that happens in href="#"

You can simply leave it empty: href=""

Liban
  • 101
  • 1
  • 2