26

This is the ajax

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

This works, but i want the url to change because I have many parameters there and of course with the data: "urut="+$(".urut").val(), parameter as well.

Andy Hoffman
  • 18,436
  • 4
  • 42
  • 61
Dimas Adi Andrea
  • 443
  • 3
  • 11
  • 25
  • if you change url it wil reload. sorry if i misunderstood your question. if i did please clarify – guradio Jun 29 '16 at 02:48
  • Why would you reload a page because of needing to add more parameters to an `ajax` request? I'm not sure I understand. (Side note, did you know you can pass an object with key-value pairs?) – Assimilater Jun 29 '16 at 02:48
  • 2
    `history.pushState(). history.replaceState()` https://developer.mozilla.org/en-US/docs/Web/API/History_API – ArtisticPhoenix Jun 29 '16 at 02:49
  • Possible duplicate of [Modify the URL without reloading the page](http://stackoverflow.com/questions/824349/modify-the-url-without-reloading-the-page) –  Jun 29 '16 at 06:24

3 Answers3

43

You can do this to your success action :

window.history.pushState("object or string", "Title", "/new-url");

See this post to Modify the URL without reloading the page for a basic how-to.

Additional Note:

  1. The first parameter is the data we'll need if the state of the web page changes, for instance whenever someone presses the back or forwards button in their browser. Note that in Firefox this data is limited to 640k characters.
  2. title is the second parameter which can be a string, but at the time of writing, every browser simply ignores it.
  3. This final parameter is the URL we want to appear in the address bar.

It's now available in most "modern" browsers.

Sherly Febrianti
  • 1,097
  • 15
  • 33
6

Use the browser history to change the url bar in JS.

      history.pushState()
     history.replaceState()

https://developer.mozilla.org/en-US/docs/Web/API/History_API

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
6

try this

 $(".urut").change(function() {
    $.ajax({
        url: "<?php echo base_url(); ?>categories/brand/<?= $link_brand; ?>?l=<?= $l; ?>&h=<?= $h; ?>&city=<?= $city; ?>&city_name=<?= $city_name; ?>&ket=view",
        type: "POST",
        data: "urut=" + $(".urut").val(),
        success: function(data) {
            $("#result").html(data);
            window.history.pushState("Details", "Title", "<?php echo base_url(); ?>/yourNewPage");
        }
    });
});
s.kuznetsov
  • 14,870
  • 3
  • 10
  • 25
ANIK ISLAM SHOJIB
  • 3,002
  • 1
  • 27
  • 36