通过javascript从URL中删除未使用的char

时间:2017-05-28 16:21:38

标签: javascript

我正在尝试从网址中删除自定义参数,但我做错了

有我的javascript

var removeFilterHelper = function(param) {
            var loc = location.protocol + '//' + location.host + '/Project/Search' + location.search;
            location.href = loc
                .replace(param, '');
}

例如,如果我喜欢这个网址:

http://localhost:5000/Project/Search/?categoryId=50,categoryId=20&subCategoryId=10

removeFilterHelper( “的categoryId = 50”);

结果如下:

http://localhost:5000/Project/Search/?,categoryId=20&subCategoryId=10

1 个答案:

答案 0 :(得分:0)

只需在您的参数中添加逗号即可。



var loc = "http://localhost:5000/Project/Search/?categoryId=50,categoryId=20&subCategoryId=10";
var param ="categoryId=50" + ','; // Also remove the trailing comma
console.log(loc.replace(param, ''))