This repository has been archived by the owner on Feb 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.urlmod.v1.0.js
83 lines (63 loc) · 2.45 KB
/
jquery.urlmod.v1.0.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/** @preserve Author: Greg Whitworth */
/** @preserve URL: https://github.com/gregwhitworth/urlmod */
/** @preserve Version: 1.0 */
/** @license MIT License */
(function ( $ ) {
$.urlmod = function(options) {
var settings = $.extend( {}, $.urlmod.defaults, options );
if(settings.url != (null || "")) {
var url = settings.url;
var updates = settings.updates;
var urlStart = url.substring(0, url.indexOf('?') + 1); // This will get everything through the ?
var paramString = url.substring(url.indexOf('?') + 1); // This will get everything after the ?
var paramsArr = []; // Used to store the params we want to work with
var initparams = paramString.split('&'); // The initial param strings
var finalparams = [] // An array to store the final params
// Setup the initial param array from
// the URL
$.each(initparams, function(i, param) {
var keyVal = param.split('=');
var key = keyVal[0];
var val = keyVal[1];
paramsArr[key] = val;
});
// Get the keys of the array for us
// to reference later
var keys = Object.keys(paramsArr);
// Remove any items that do not have updates
// if the user wants them removed
if(settings.removeStaleParams) {
$.each(keys, function(i, key) {
if(eval('updates.' + key) == undefined) {
delete paramsArr[key];
}
});
}
// Update the values of the array with
// our updates
$.each(updates, function(key, update) {
if($.isArray(update)) {
update = update.join(',');
}
paramsArr[key] = update;
});
// We need to get the keys of the array
// again since we may have removed/added items
keys = Object.keys(paramsArr);
// Loop through the keys and create
// the final strings for use in the url
$.each(keys, function(i, key) {
finalparams.push(key + '=' + paramsArr[key]);
});
// Return the final url
return urlStart + finalparams.join('&');
}
return; // Return nothing as the URL wasn't supplied
}
// The default options
$.urlmod.defaults = {
url: null, // Provide the URL with parameters to parse out, at minimum: ?param1=value
updates: {}, // An object with the params you would like to update using the param name as the key
removeStaleParams: false // If set to true, if the item isn't update the param will not be included upon return
}
}( jQuery ));