How to change url after success in ajax without page reload

How to change url after success in ajax without page reload

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to Change URL After Success in AJAX Without Page Reload

Changing the browser's address bar after an asynchronous request succeeds is a common requirement for single-page applications, but it introduces a specific challenge: you cannot simply update the window.location property inside the success callback if that same change causes the page to reload or reset the JavaScript context. In your current setup, the URL contains complex server-side parameters like $l, $h, and $city, which suggests a dynamic routing scenario often found in legacy PHP applications using GET requests for state management.

The core issue with your existing code lies in how jQuery handles data transmission versus how modern browsers interpret URLs. You are currently sending the urut parameter via the data property of the AJAX object:

$.ajax({
  url: "<?php echo base_url(); ?>categories/brand/<?php echo $link_brand; ?>?l=<?php echo $l; ?>&h=<?php echo $h; ?>&city=<?php echo $city; ?>&city_name=<?php echo $city_name; ?>&ket=view",
  type: "POST",
  data: "urut=" + $(".urut").val(),
  success: function(data) {
    $("#result").html(data);
  }
})

When you append &data=... to the URL string, the browser sends that information as part of the request URI. However, when you send data via the data property with a POST type, it gets appended to the request body, not the URL. This creates a disconnect between what the user sees and what the server receives regarding the dynamic state.

To update the URL without reloading the page, you must manipulate the browser's history stack using the HTML5 History API. The most straightforward approach is to use history.pushState or history.replaceState. Since you want to reflect a new state after a successful action, pushState is usually preferred if this represents a distinct step in navigation.

Here is how you can integrate the URL change directly into your success callback:

$(".urut").change(function() {
  $.ajax({
    url: "<?php echo base_url(); ?>categories/brand/<?php echo $link_brand; ?>&l=<?php echo $l; ?>&h=<?php echo $h; ?>&city=<?php echo $city; ?>&city_name=<?php echo $city_name; ?>&ket=view&urut=" + $(".urut").val(),
    type: "POST", // Or GET, depending on your server logic
    data: {}, // We moved the 'urut' parameter into the URL string
    success: function(data) {
      $("#result").html(data);

      // Construct the new URL including all parameters
      var currentUrl = window.location.href.split('?')[0]; 
      var newParams = "?l=" + l + "&h=" + h + "&city=" + city + "&city_name=" + city_name + "&ket=view&urut=" + $(".urut").val();
      
      // Update the URL and history stack
      history.pushState({
        url: currentUrl + newParams,
        urut: $(".urut").val()
      }, '', currentUrl + newParams);
    }
  });
});

Notice that in this implementation, we moved the urut parameter into the URL string itself rather than the request body. This ensures the URL reflects the actual data being transmitted. We then use history.pushState to update the address bar without triggering a full page load. The third argument of pushState is crucial: it must match the href you want displayed in the browser.

There are significant trade-offs to consider here. Updating the URL manually does not automatically notify search engines or external links that the content has changed unless you also handle back-button behavior correctly. Furthermore, if your backend relies on GET requests to fetch data based on these parameters, switching to POST while trying to maintain a GET-like URL can lead to conflicts.

A more robust architectural pattern in frameworks like Laravel (often associated with sites using domains like https://laravelcompany.com) is to rely on route parameters instead of query strings for state. If your application allows it, consider defining a route that accepts the urut value as part of the URL path or query string directly, then use route() helpers to generate the correct link. This decouples the frontend logic from manual string concatenation and reduces the risk of parameter mismatch between the client and server.

Additionally, be aware that some older browsers or specific configurations might ignore pushState if the server does not support HTML5 History mode. If you are targeting a wider audience, ensure your fallback behavior (like standard form submission) is in place, though for pure AJAX interactions, the History API remains the standard solution for persistent state management without reloads.

Our network