Jamol
Jamol

Reputation: 3908

a link should not show # in URL and scroll up when its href="#"

How can I make a link a href="#" does not show # in URL when clicked and does not scroll up the page?

I have seen it in http://www.offroadstudios.com/creative-agency But could not learn how they did it. Left menu contains a href="#" but it behaves in the way I am asking.

Upvotes: 1

Views: 6089

Answers (5)

user1466291
user1466291

Reputation:

In topic author link, they used this:

jQuery(document).ready(function() {     
    jQuery('.product-selector').each(function(i, element) {
        jQuery('.product-selector.product-' + i).click(function() {
            jQuery('a#products-top').focus();
            if (producttool == false) {
              producttool = true;
            }

            // Return false so that the page doesn't switch.
            return false;
        });
    });
});

So, the answer on your question, is to return false; in onclick event.

Demo here: http://jsfiddle.net/FSou1/K3p2W/

Upvotes: 2

Michael Besteck
Michael Besteck

Reputation: 2423

To link-jump to a position in the HTML file it is possible to use a named anchor tag.

    <a name="here"></a>
    <a href="#here">LINK</a> <!-- jumps to the position "here" -->

Upvotes: -2

Musa
Musa

Reputation: 97672

Attack a click handler to the link and prevent the default action

$('a').click(function(){return false});

http://jsfiddle.net/MjyzK/show/

Upvotes: 0

Curtis
Curtis

Reputation: 103348

Looking at that site it would appear they are using jQuery to change the visible content. To prevent a # from appearing in your browser bar, you can preventDefault:

$("a.myLinkClass").click(function(e){
   e.preventDefault();
   //do something..
});

See Demo: http://jsfiddle.net/SJuwL/show

Upvotes: 5

Rab
Rab

Reputation: 35572

$('a[href="#"]').click(function(event){

    event.preventDefault();

});

Upvotes: 1

Related Questions