js 設置url 參數值


//設置url中參數值
function setParam(param,value){
    var query = location.search.substring(1);
    var p = new RegExp("(^|)" + param + "=([^&]*)(|$)");
    if(p.test(query)){
        //query = query.replace(p,"$1="+value);
        var firstParam=query.split(param)[0];
        var secondParam=query.split(param)[1];
        if(secondParam.indexOf("&")>-1){
            var lastPraam=secondParam.split("&")[1];
            return  '?'+firstParam+'&'+param+'='+value+'&'+lastPraam;
        }else{
            if(firstParam){
                return '?'+firstParam+'&'+param+'='+value;
            }else{
                return '?'+param+'='+value;
            }
        }
    }else{
        if(query == ''){
            return '?'+param+'='+value;
        }else{
            return '?'+query+'&'+param+'='+value;
        }
    }    
}

//調用
var url=window.location.href;//獲取當前url
var searchUrl=setParam("beginDate",date);
if(url.indexOf("?")>0){
    =url.split("?")[0];
}
window.location.href=url+searchUrl;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM