D.Nursultan
D.Nursultan

Reputation: 55

How to change url so that the current HTML remains

By clicking a button, the url has to change but html should stay the same

$('#v-pills-profile-tab').on('click', function (event) {
   // change url
   // and html remains
})

Upvotes: 1

Views: 138

Answers (2)

angel.bonev
angel.bonev

Reputation: 2232

You can use History.pushState() and WindowEventHandlers.onpopstate

<button id="v-pills-profile-tab"> TEST ME </button>
<script>
    $('#v-pills-profile-tab').on('click', function (event) {
        window.history.pushState({data: "some data that i will need"}, "", "my-new-url");
    });
    window.onpopstate = function (e) {               
        if (e.state) {
            console.log(e.state.data);
        } else {
            window.location.reload();
            return;
        }
    };
</script>

Upvotes: 4

Jalaj
Jalaj

Reputation: 463

For changing url, you can simply do:

window.location = //Your url here

Also, For reloading the page, you can simply use:

window.location.reload()

Note

When you change the url, the page reloads itself on its own. You don't have to reload it

Upvotes: 0

Related Questions