在没有页面重新加载的情况下向URL添加(获取)参数?

时间:2017-03-27 11:02:39

标签: javascript jquery

使用我的代码,我将每个选定的元素推送到数组。我可以使用像window.location.href这样的javascript将其推送到网址参数。我可以在没有页面重新加载/重定向的情况下将params添加到url吗?

var keys = [];
keys.push({selected_element:object.key});

需要的结构是:/index.php?element[]=546454&element[]=156151&element[]=343141

1 个答案:

答案 0 :(得分:3)

您可以使用history.pushState ...:

if (history.pushState) 
{
    var newQueryString = window.location.protocol + "//" + 
    window.location.host + window.location.pathname + 
    '?newStuff[]=123&whatever=909';

    window.history.pushState({path:newQueryString},'',newQueryString);
}
相关问题