1

I want to add a parameter to url. Here problem is when I click a tag history.pushState is working but after removing parameter. Firstly, history.pushState is changing to /general-info-21,2231-44,333 after url changing to /general-info. How can I solve?

HTML

<ul>
    <li><a href="/general-info" data-desc="general-info" onclick="dataInfo($event)">General Info</a></li>
</ul>

JS

function dataInfo($event){
    var desc = $($event.target).attr("data-desc");
    lat="21,2231";
    lng="44,333";
    history.pushState(null, null, desc + "-" + lat + "-" + lng);  
}
1
  • 1
    Perhaps it hapens, because the a tag does what it should. In your function you can try to disable the default behaviour using $event.preventDefault(); at the top of the handler. Commented Jul 22, 2018 at 20:19

1 Answer 1

1

I just put here the possible solutions.

Way 1. You can disable the default behaviour of the tag a using $event.preventDefault(); in your handler.

Way 2. You can set href="#" instead of href="/general-info" and it will not take you to another route

Sign up to request clarification or add additional context in comments.

2 Comments

I have to use first way for seo. Thanks.
Nice choice. Happy to help!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.