[英]Get the relative path of the URL with all the parameters in Javascript?
I have an URL like below format: 我的网址格式如下:
http://localhost:8080/search/?N=4294967292&Nr=OR%28db_Analysis_technique%3AGC%29&Ntt=agilent
I wanted to get the expected relative URL like below format: 我想获取预期的相对URL,如下所示:
search/?N=4294967292&Nr=OR%28db_Analysis_technique%3AGC%29&Ntt=agilent
Please find my piece of code to get the relative path but it doesn't return the parameters... 请找到我的代码以获取相对路径,但它不返回参数...
var pathArray = window.location.pathname.split( '/' );
var newPathname = "";
for (i = 0; i < pathArray.length; i++) {
newPathname += "/";
newPathname += pathArray[i];
}
console.log(newPathname);
any help on this.. thanks 任何帮助..谢谢
To access the query parameters you'll be wanting window.location.search
- this will include the ?
要访问查询参数,您需要
window.location.search
这将包括?
separator, if needed. 分隔符(如果需要)。
var newPathName = window.location.pathname.substr(1) + window.location.search;
Try with the below code :- 尝试以下代码:-
var pathArray[] = window.location.pathname.split( '/' );
var newPathname = "";
for (i = 0; i < pathArray.length; i++) {
newPathname += "/";
newPathname += pathArray[i];
}
console.log(newPathname);
http://jsbin.com/biseradi/1/edit 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.