此jQuery实用程序函数检查当前页面URL中是否存在参数,如果不存在,则将其添加并完整返回URL。 如果您需要执行ajax请求以使用新的表单数据更新数据库,并且只想重定向到相同的url,但带有更新的标志以显示一个更新的框,则可能很有用。
(function($,W,D)
{
var JQUERY4U = {};
JQUERY4U.UTIL =
{
/**
* Add a parameter to url if doesn't already exist
* @param param - the parameter to add
* @param value - the value of the parameter
* @return url - the url with the appended parameter
*/
addParamToUrl: function(param, value)
{
//check if param exists
var result = new RegExp(param + "=([^&]*)", "i").exec(W.location.search);
result = result && result[1] || "";
//added seperately to append ? before params
var loc = W.location;
var url = loc.protocol + '//' + loc.host + loc.pathname + loc.search;
//param doesn't exist in url, add it
if (result == '')
{
//doesn't have any params
if (loc.search == '')
{
url += "?" + param + '=' + value;
}
else
{
url += "&" + param + '=' + value;
}
}
//return the finished url
return url;
}
}
//example usage
var updatedUrl = JQUERY4U.UTIL.addParamToUrl('updated', 'true');
console.log(updatedUrl);
//input: http://jquery4u.com/index.php
//output: http://jquery4u.com/index.php?updated=true
})(jQuery, window, document);
From: https://www.sitepoint.com/jquery-add-param-url-function/