3

I want hash-tags to be removed from URL after they are used. For example, when i click on the link below:

<a href="#btnq1"><button type="button" name="" value="" id="btnq1">Just a button</button></a>

I want the hash-tag #btnq1 that appears to the URL of the page to be removed just after the action on this link happens. I tried the below jquery code with no success:

$('#btnq1').click(function(event){
   event.preventDefault();
   // your action
});

And even if this works, then how do i implement it to work for every hash tag that is added to the URL? I would like to solve it using javascript.

Dchris
  • 2,867
  • 10
  • 42
  • 73
  • 1
    possible duplicate: http://stackoverflow.com/questions/10026223/removing-hash-from-url – VF_ Apr 26 '14 at 14:10
  • i've picked up a wrong question. – Zafar Apr 26 '14 at 14:21
  • @user3348022 I don't see a solution to your possible duplicate.. I would be glad if you can show me.. – Dchris Apr 26 '14 at 14:22
  • Why do you have a link there? It is invalid, will break things in some browsers, and you are binding your click event to a button. – Quentin Apr 26 '14 at 14:27
  • 1
    Having a button inside an anchor. http://validator.w3.org/ is a useful tool. – Quentin Apr 26 '14 at 14:33
  • No, it isn't. HTML 5 expressly forbids it. – Quentin Apr 26 '14 at 14:35
  • 1
    Content Model: Transparent, but there must be no interactive content descendant. — http://www.w3.org/TR/html5/text-level-semantics.html#the-a-element (a button, something designed to be clicked on, is interactive content) – Quentin Apr 26 '14 at 14:36

2 Answers2

7

You could try that:

$(window).on('hashchange', function(e){
    history.replaceState ("", document.title, e.originalEvent.oldURL);
});
A. Wolff
  • 74,033
  • 9
  • 94
  • 155
0

first add a class to your a tag that you want this behavior for, or a html 5 data- attribute. Then your link becomes;

<a href="#btnq1" class="remove-hash"><button>Button</button></a>

$('body').on('click', ".remove-hash", function(e){
    $(this).removeAttr('href');
});
tcooc
  • 20,629
  • 3
  • 39
  • 57
Honorable Chow
  • 3,097
  • 3
  • 22
  • 22