Javascript 在url中追加参数而不重新加载页面

Javascript 在url中追加参数而不重新加载页面,javascript,jquery,url,append,pushstate,Javascript,Jquery,Url,Append,Pushstate,我使用以下代码将param附加到url。这工作正常,但当在url中追加参数时,页面将被重新加载。我想在不重新加载页面的情况下使用此功能 function insertParam(key, value) { key = escape(key); value = escape(value); var kvp = document.location.search.substr(1).split('&'); var i=kvp.length; var x; whil

我使用以下代码将param附加到url。这工作正常,但当在url中追加参数时,页面将被重新加载。我想在不重新加载页面的情况下使用此功能

function insertParam(key, value)
{
    key = escape(key); value = escape(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('=');
                    alert('sdfadsf');
                break;
        }
    }

    if(i<0) {kvp[kvp.length] = [key,value].join('=');}

    //this will reload the page, it's likely better to store this until finished
    document.location.search = kvp.join('&'); 
    //alert(document.location.href);
函数insertParam(键,值)
{
键=转义(键);值=转义(值);
var kvp=document.location.search.substr(1).split('&');
var i=kvp.length;var x;while(i--)
{
x=kvp[i]。拆分('=');
如果(x[0]==键)
{
x[1]=数值;
kvp[i]=x.join('=');
警报(“sdfadsf”);
打破
}
}

如果(i您只能使用HTML5功能来执行此操作。

您只能使用HTML5功能来执行此操作。

有一个新功能,旨在用更好的解决方案取代
location.hash
的使用:
pushState

 window.history.pushState(data, "Title", "/new-url");

更多信息:

有一个新功能,旨在用更好的解决方案取代
location.hash
的使用:
pushState

 window.history.pushState(data, "Title", "/new-url");

更多信息:

我想在url中添加mutliple参数,如:我想在url中添加mutliple参数,如:我认为OP只想在url中添加片段,例如
document.location.href+='#txt'
@NADH他从未说过-代码(以及原始问题)谈到了查询参数,而不是锚定片段。我认为OP只想将片段附加到url,例如
document.location.href+='#txt'
@NADH他从未说过-代码(和原始问题)谈到了查询参数,而不是锚定片段。