0

With my code below I push every selected element to a array. I can push it to a url param with javascript like window.location.href. Can I add params to url without page reloading/redirect?

var keys = [];
keys.push({selected_element:object.key});

Needed structure is: /index.php?element[]=546454&element[]=156151&element[]=343141

samius polis
  • 379
  • 5
  • 17
  • http://stackoverflow.com/questions/824349/modify-the-url-without-reloading-the-page http://stackoverflow.com/questions/3338642/updating-address-bar-with-new-url-without-hash-or-reloading-the-page – NullDev Mar 27 '17 at 11:05

1 Answers1

3

You can use history.pushState...:

if (history.pushState) 
{
    var newQueryString = window.location.protocol + "//" + 
    window.location.host + window.location.pathname + 
    '?newStuff[]=123&whatever=909';

    window.history.pushState({path:newQueryString},'',newQueryString);
}
Stuart
  • 6,630
  • 2
  • 24
  • 40
  • @user2956944 Glad to help - do tick as an accepted answer if it answers your question - cheers – Stuart Mar 27 '17 at 11:14