Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to create an <a> link with empty href [duplicate]

I want to create a link , and to force the link anchor using javaScript, i tried the following:-

<a id="ServerSort" href="#" style="text-decoration:underline">

it worked well but the page will loose its current position after clicking on the <a> link.

I need the link to have an href, because i need need the mouse to change the cursor when going over it ?

can anyone advice ?

like image 750
John John Avatar asked Apr 08 '14 14:04

John John


People also ask

What has a link with an empty href attribute?

This means that the URL in question contains at least one outgoing anchor link which has an empty href attribute.

What does empty href mean?

Empty HREF links basically mean re-navigate the current page. It's like a Refresh operation which resubmits to the server.

How can I use href without tag?

An <a> element without an [href] attribute is still valid. As far as semantics and styling is concerned, the <a> element isn't a link ( :link ) unless it has an [href] attribute. A side-effect of this is that an <a> element without [href] won't be in the tabbing order by default.


5 Answers

You can use javascript:void(0)

<a id="ServerSort" href="javascript:void(0)">

you don't need style="text-decoration:underline" as commented by King King


by jQuery you can do it by event.preventDefault()

Stop the default action of the event .

$('#ServerSort').click(function(event){
   event.preventDefault(); //or return false;
});
like image 132
Tushar Gupta - curioustushar Avatar answered Sep 30 '22 22:09

Tushar Gupta - curioustushar


The best way to do that is to set the href to #0 like this:

<a href="#0">blabla</a>

Here's why: You will NEVER have anything on the page with the ID 0 (if you do, there's a problem there anyways), and since #0 doesn't exist, the click event doesn't bring you back to the top of page. You do NOT need JavaScript to do that and you should not use JavaScript.

like image 28
Bene Avatar answered Sep 30 '22 20:09

Bene


 .clickable {
   cursor: pointer;
 }
 <a id="ServerSort" class="clickable">Foo</a>
like image 26
James Sumners Avatar answered Sep 30 '22 22:09

James Sumners


A few options exist:

<a id="ServerSort" href="javascript:">

<a id="ServerSort" href="javascript://">

<a id="ServerSort" href="javascript:void(0)">

These are considered bad practice and I would never recommend something like this for production. I typically use the first option when linking to pages that don't exist during the development phase.

Preferably, I would not use a at all, I switch to a span in example below:

<span id="ServerSort" onclick="return false">Test Link</span>

and just style an element accordingly:

    <style type="text/css">
    #ServerSort {
        text-decoration:underline;
    }
        #ServerSort:hover {
            color:red;
            cursor:pointer;
        }
</style>

I am in-lining the js and just hand writing a here to show a different approach which avoid the need to override the default behavior.

like image 31
justinlabenne Avatar answered Sep 30 '22 22:09

justinlabenne


Use Event.preventDefault()

This method tells the user agent that if the event does not get explicitly handled, its default action should not be taken as it normally would be.

function clickme(ev) {
  alert('You stay here!')
  ev.preventDefault()
}
<a onclick='clickme(event)' href='https://www.google.com/'>https://www.google.com</a>

The event continues to propagate as usual, unless one of its event listeners calls stopPropagation() or stopImmediatePropagation(), either of which terminates propagation at once.


Alternatively, you can use onclick='return false' (either inline or in a function). It will prevent default browser behaviour (more info):

function clickme() {
  alert('You stay here!');
  return false;
}
<a href="https://www.google.com/" onclick='return clickme()'>https://www.google.com/</a>

This approach prevents the event to propagate, though. More details.

like image 36
Aray Karjauv Avatar answered Sep 30 '22 20:09

Aray Karjauv