Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change url after success in ajax without page reload

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.

like image 265
Dimas Adi Andrea Avatar asked Jun 29 '16 02:06

Dimas Adi Andrea


People also ask

How do I change the URL after Ajax success?

You can do this to your success action : window. history. pushState("object or string", "Title", "/new-url");

How do I redirect to another page without refreshing?

This is simple you just need change the URL In HTML5 as : window. history. pushState("object or string", "Title", "/new-url");

How do I use JavaScript to modify the URL without reloading the page?

the page using JavaScript? the page using JavaScript? Method 2: Adding a new state with pushState() Method: The pushState() method is used to add a new history entry with the properties passed as parameters. This will change the current URL to the new state given without reloading the page.


2 Answers

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.

like image 176
Sherly Febrianti Avatar answered Oct 07 '22 09:10

Sherly Febrianti


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

like image 41
ArtisticPhoenix Avatar answered Oct 07 '22 10:10

ArtisticPhoenix