javascript - 在不重新加载页面的情况下在 url 中 append 参数

标签 javascript jquery url append pushstate

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

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);

我想在不重新加载页面的情况下向 url 添加多个参数:

  • txt1
  • txt2
  • txt3
  • 链接1
  • 链接2
  • 链接3

我想要网址:“..../search.php”

点击txt2后 我想要网址:“..../search.php#t_2”

点击链接后 我想要网址:“..../search.php#t_1&l_2”

最佳答案

您只能使用 history.pushState(state, title, url) 执行此操作这是一项 HTML5 功能。

关于javascript - 在不重新加载页面的情况下在 url 中 append 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10445229/

相关文章:

javascript - 制作指定范围的数组

javascript - 当失去焦点时如何让 setInterval 运行?

javascript - 从Facebook的移动应用程序复制标题滚动效果

c# - 处理 URL 中的媒体文件

javascript - angular.js 更改 url 而不调用 Controller

javascript - scrolltop 不会滚动到底部 div

javascript - 使用 Javascript 将复杂的 HTML 动态添加到 div

jquery - 最大高度过渡

jquery - 在 jQuery 中选择类中的下一个实例

regex - 是否有仅包含小写字母数字字符和连字符的字符串的名称?