0

Let assume I'm on page www.example.com. Can I add /first to the URL so that it becomes www.example.com/first on click WITHOUT refreshing the page. I'm trying to do this after an AJAX request which changes the page, so that I could indicate the page has been changed.

For example:

$('.admin-categories').on('click', function(){
    $.ajax({
        type: 'POST',
        url : adminCategories,
        headers: {
            'X-CSRF-TOKEN': token
        },
        success : function (data) {
            $(".admin-container").html(data);
        }
    });
});

Can I append /categories to the URL on success of this AJAX call?

Onyx
  • 5,186
  • 8
  • 39
  • 86

1 Answers1

1

You're looking for the History API in JavaScript and replaceState method specifically.

history.replaceState(null, null, 'first');

Here is a good resource about it: https://css-tricks.com/using-the-html5-history-api

Jason Byrne
  • 1,579
  • 9
  • 19
  • Hmmm, while this does change the URL, it doesn't just append it but also replaces what I currently have. For example, I have www.domain.com/admin and instead of changing the URL to www.domain.com/admin/first, it changes it to www.domain.com/first. – Onyx Dec 22 '18 at 06:44
  • Also I'm not sure why, but your link redirects me to something related to CSS – Onyx Dec 22 '18 at 06:44
  • Fixed the link... and you can do the full path: history.replaceState(null, null, '/admin/first'); – Jason Byrne Dec 22 '18 at 06:46