// 添加参数&key=value 直接输出url
function insertParam(key, value)
{
key = encodeURI(key); value = encodeURI(value);
var kvp = document.location.search.substr(1).split('&');
var i=kvp.length; var x; while(i--)
{
x = kvp[i].split('=');
if (x[0]==key)
{
x[1] = value;
kvp[i] = x.join('=');
break;
}
}
if(i<0) {kvp[kvp.length] = [key,value].join('=');}
document.location.search = kvp.join('&');
}
//直接把参数添加到尾部
function addParameterToURL(param){
_url = location.href;
_url += (_url.split('?')[1]? '&':'?') + param;
return _url;
}
// 判断& ? 在后面选择性添加
var addUrlParam = function(search, key, val){
var newParam = key + '=' + val,
params = '?' + newParam;
if (search) {
params = search.replace(new RegExp('[?&]' + key + '[^&]*'), '$1' + newParam);
if (params === search) {
params += '&' + newParam;
}
}
return params;
};
var yl_url=document.location.host + addUrlParam(document.location.search, 'foo', 'bar');