获取包含Javascript中所有参数的URL的相对路径?

获取包含Javascript中所有参数的URL的相对路径?,javascript,Javascript,我的URL格式如下: http://localhost:8080/search/?N=4294967292&Nr=OR%28db_Analysis_technique%3AGC%29&Ntt=agilent search/?N=4294967292&Nr=OR%28db_Analysis_technique%3AGC%29&Ntt=agilent 我希望获得预期的相对URL,格式如下: http://localhost:8080/search/?N=42949

我的URL格式如下:

http://localhost:8080/search/?N=4294967292&Nr=OR%28db_Analysis_technique%3AGC%29&Ntt=agilent
search/?N=4294967292&Nr=OR%28db_Analysis_technique%3AGC%29&Ntt=agilent
我希望获得预期的相对URL,格式如下:

http://localhost:8080/search/?N=4294967292&Nr=OR%28db_Analysis_technique%3AGC%29&Ntt=agilent
search/?N=4294967292&Nr=OR%28db_Analysis_technique%3AGC%29&Ntt=agilent
请查找我的代码片段以获取相对路径,但它不会返回参数

var pathArray = window.location.pathname.split( '/' );
var newPathname = "";
for (i = 0; i < pathArray.length; i++) {
  newPathname += "/";
  newPathname += pathArray[i];
}
console.log(newPathname);

这方面有什么帮助吗。。谢谢

请尝试以下代码:-

var pathArray[] = window.location.pathname.split( '/' );
var newPathname = "";
for (i = 0; i < pathArray.length; i++) {
  newPathname += "/";
  newPathname += pathArray[i];
}
console.log(newPathname);

要访问查询参数,您需要window.location.search-这将包括?分离器,如果需要

var newPathName = window.location.pathname.substr(1) + window.location.search;

为什么要在/上拆分,然后立即重新组合?基本正确,但我建议使用substr0索引0为完整的相对URL包含前面的斜杠。@Windwarker OP不需要前导斜杠。与OP的代码相同,只是它还有一个语法错误var pathArray[]不合法