使用Javascript中的所有参数获取URL的相对路径?

时间:2014-07-18 10:44:22

标签: javascript

我有一个类似以下格式的网址:

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

对此有任何帮助..谢谢

3 个答案:

答案 0 :(得分:1)

要访问您想要window.location.search的查询参数 - 如果需要,这将包括?分隔符。

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

答案 1 :(得分:-1)

尝试使用以下代码: -

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

答案 2 :(得分:-1)

http://jsbin.com/biseradi/1/edit

var pathArray = window.location.pathname.split( '/' );
var newPathname = "";
    var newPathname = "";
    for (i = 3; i < pathArray.length; i++) {

      newPathname += pathArray[i] ;
      if (i< pathArray.length-1){
        newPathname += "/";
      }

    }
    console.log(newPathname);