Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iron Router , change URL without changing browser history

Using iron router version 0.9.4 How to update URL query parameters without changing the browser history?

I have a bunch of select boxes which can update the URL query parameters. In some cases I'd like to update the URL without adding an additional entry into window.history. A lot of templates are dependent on the Router.current().params reactive variable, so I need to update that as well when changing the route.

like image 597
looshi Avatar asked Mar 15 '23 08:03

looshi


2 Answers

Iron Router has an optional parameter in the Router.go function which you can pass in {replaceState:true} . This will update the URL without storing the URL change in the window's history.

This is working in Iron Router 0.9.4

var path = Router.path('myRoute',{_id: myId}, {query: myParams});
Router.go(path, {replaceState: true});

If you are on a newer version of Iron Router, the order of arguments has probably changed a little, according to this github issue this would be the new order :

Router.go(path, {}, {replaceState:true});
like image 81
looshi Avatar answered Mar 26 '23 21:03

looshi


You can use vanilla JavaScript:

window.history.replaceState(yourNewUrl);

Here is some some documentation. As it's a modern feature, if you want to support older browsers, you can use a polyfill.

like image 21
Julien Avatar answered Mar 26 '23 22:03

Julien