Skip to content

Instantly share code, notes, and snippets.

@matason
Created March 10, 2014 09:54
Show Gist options
  • Save matason/9462228 to your computer and use it in GitHub Desktop.
Save matason/9462228 to your computer and use it in GitHub Desktop.
jQuery(document).ready(function($) {
var parametersToPersist = ['region', 'community', 'content_types', 'page'];
var currentParams = getUrlVars(window.location.href);
var queryString = buildQuery(currentParams);
console.log(decodeURIComponent(queryString));
jQuery("a").not("a.ext").each(function(){
var href = jQuery(this).attr('href');
if (href) {
href += (href.match(/\?/) ? '&' : '') + queryString;
if (href.indexOf('?') != -1) {
var split_href = href.split('?');
if (split_href[0].indexOf('#') != -1) {
href = split_href[0];
}
}
jQuery(this).attr('href', href);
}
});
function getQueryStringParams(url) {
var params = [];
if (url.indexOf('?') != -1) {
params = url.slice(url.indexOf('?') + 1).split('&');
}
return params;
}
function getUrlVars(url) {
var result = [];
var hash = [];
var hashes = getQueryStringParams(url);
for (var hashCount = 0; hashCount < hashes.length; hashCount++) {
hash = hashes[hashCount].split('=');
// Limit the parameters that can be added.
for (var parameterCount = 0; parameterCount < parametersToPersist.length; parameterCount++) {
var parameterToPersist = parametersToPersist[parameterCount];
if (hash[0].indexOf(parameterToPersist) != -1) {
result[parameterToPersist + '[' + hash[1] + ']'] = hash[1];
}
}
}
return result;
}
function buildQuery(parameters) {
var qs = "";
var queryString = '';
for (var key in parameters) {
var value = parameters[key];
qs += encodeURIComponent(key) + "=" + encodeURIComponent(value) + "&";
}
if (qs.length > 0) {
qs = qs.substring(0, qs.length - 1); // Remove last "&"
queryString = queryString + "?" + qs;
}
return queryString;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment