lenye_base
Version:
基础方法
29 lines (22 loc) • 815 B
JavaScript
import './ifnodeorbrowser.js';
import { windows } from './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] : 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;
}
};
export default setUrlParam;