lenye_base
Version:
基础方法
31 lines (23 loc) • 855 B
JavaScript
;
require('./ifnodeorbrowser.js');
var windowsimulate = require('./windowsimulate.js');
/**
* setUrlParam
* From https://stackoverflow.com/questions/5999118/add-or-update-query-string-parameter
*/
var setUrlParam = function (key, value) {
var url = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : windowsimulate.windows.location.href;
var re = new RegExp('([?|&])' + key + '=.*?(&|#|$)', 'i');
if (url.match(re)) {
return url.replace(re, '$1' + key + '=' + encodeURIComponent(value) + '$2');
} else {
var hash = '';
if (url.indexOf('#') !== -1) {
hash = url.replace(/.*#/, '#');
url.replace(/#.*/, '');
}
var separator = url.indexOf('?') !== -1 ? '&' : '?';
return url + separator + key + '=' + encodeURIComponent(value) + hash;
}
};
module.exports = setUrlParam;