Hermes
Hermes

Reputation: 462

Adding a parameter to url javascript

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);  
}

Upvotes: 1

Views: 69

Answers (1)

Max Martynov
Max Martynov

Reputation: 739

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

Upvotes: 1

Related Questions