Extra Browsers.Extras
Aaron Newton at Clientcide made a very useful extension on the MooTools Browser script: Browser.Extras. Although it contains a just a few functions, it is very handy. For example, Browser.getQueryStringValues(), to read variables from the URI (known in PHP as $_GET). Really nice to have.
When writing the PicasaViewer, I needed to set the href attribute for each album: basically I only needed to add something like '?albumid=1234'. Easy, except when you use a <base> element, or when albumid is already in your query string.
So now we have extra Browser.Extras: browser.Extras.setQueryStringValues.js
This function uses your current window.location.href and an object with values, adds the values to the query string (or replaces when they already exist) and returns the href you need.
$extend(Browser, { setQueryStringValues: function(values){ var base = $pick(window.location.href, window.location.search, '').split('?')[0]; //get the base string var qs = this.getQueryStringValues(); // get the query string var merged = $merge(qs, values); var query = '?'; for (key in merged) { query += '&' + key + '=' +merged[key] } return base + query; } });
Example usage:
//window.location is http://www.example.com/?red=apple&ayellow=lemon Browser.setQueryStringValues({'pink':'dragonfruit'}); // returns http://www.example.com/?red=apple&yellow=lemon&pink=dragonfruit Browser.setQueryStringValues({'pink':'dragonfruit', 'red': 'strawberry'}); // returns http://www.example.com/?red=strawberry&yellow=lemon&pink=dragonfruit
You can download the script here: browser.Extras.setQueryStringValues.js