javascript - Managing queries with URI.js -


my goal able manage , modify query parameters in url. decided give uri.js shot.

when page first loads, has parameter: http://localhost:3000/football?game=this-game. want to append parameters http://localhost:3000/football?game=this-game&foo=bar&fiz=buzz.

i can't quite figure out how uri.js, whilst keeping first part of param. suppose i'll have pull first param object on page load, , reset it, hoping easier way.

previously trying uri.js, using pushstate , grabbed current window.location plus whatever params wanted added, formed string, , pushed url

if (typeof (history.pushstate) != 'undefined') { var obj = { title: title, url: url }; history.pushstate(obj, obj.title, obj.url); }

i'd still use pushstate (since think it's way, although uri.js docs don't talk it), have parameters object can add to, remove, etc. whenever please , automatically updates url.

i'm playing around code, result http://localhost:300/game=isc-world-tournament&foo=bar (missing first param).

var url = new uri; url.addquery({foo: 'bar'})  if (typeof (history.pushstate) != 'undefined') {     var obj = { title: 'test', url: url._parts.query };     history.pushstate(obj, obj.title, obj.url); } 

in summary, think i'm on right track, hoping easy way manage params.

you can use setquery method update existing parameters , add new ones.

from link:

var uri = new uri("?hello=world"); uri.setquery("hello", "mars"); // returns uri instance chaining // uri == "?hello=mars"  uri.setquery({ foo: "bar", goodbye : ["world", "mars"] }); // uri == "?hello=mars&foo=bar&goodbye=world&goodbye=mars" 

Comments