[英]How do I make the right conditions for the url based on the value of a variable? (javascript)
My script javascript like this : 我的脚本javascript是这样的:
<script>
var url = 'http://my-app.test/item';
var sessionBrand = 'honda';
var sessionModel = 'jazz';
var sessionCategory = 'velg';
var sessionKeyword = 'RS 175/60 R 15';
if(sessionBrand)
var brand = '?brand='+sessionBrand;
else
var brand = '';
if(sessionModel)
var model = '&model='+sessionModel;
else
var model = '';
if(sessionCategory)
var category = '&category='+sessionCategory;
else
var category = '';
if(sessionKeyword)
var keyword = '&keyword='+this.sessionKeyword;
else
var keyword = '';
var newUrl = url+brand+model+category+keyword;
console.log(newUrl);
</script>
The result of console.log like this : console.log的结果如下:
http://my-app.test/item?brand=honda&model=jazz&category=velg&keyword=RS 175/60 R 15
var sessionBrand, sessionModel, sessionCategory and sessionKeyword is dynamic. var sessionBrand,sessionModel,sessionCategory和sessionKeyword是动态的。 It can change.
它可以改变。 It can be null or it can have value
它可以为null或可以具有值
I have a problem 我有个问题
For example the case like this : 例如这样的情况:
var sessionBrand = '';
var sessionModel = '';
var sessionCategory = '';
var sessionKeyword = 'RS 175/60 R 15';
The url to be like this : 网址是这样的:
http://my-app.test/item&keyword=RS 175/60 R 15
Should the url like this : 网址应该像这样:
http://my-app.test/item?keyword=RS 175/60 R 15
I'm still confused to make the condition 我还是很困惑
How can I solve this problem? 我怎么解决这个问题?
Just use the array for params and then join them with &
separator. 只需将数组用于参数,然后将它们与
&
分隔符结合在一起即可。 For example: 例如:
var url = 'http://my-app.test/item';
var sessionBrand = 'honda';
var sessionModel = 'jazz';
var sessionCategory = 'velg';
var sessionKeyword = 'RS 175/60 R 15';
var params = [];
if (sessionBrand) {
params.push('brand=' + sessionBrand);
}
if (sessionModel) {
params.push('model=' + sessionModel);
}
if(sessionCategory) {
params.push('category=' + sessionCategory);
}
if(sessionKeyword) {
params.push('category=' + sessionCategory);
}
var newUrl = url + '?' + params.join('&');
console.log(newUrl);
The problem with your code is that it is prefacing all query parameters with a &
- except for the sessionBrand
. 您的代码的问题在于,除了
sessionBrand
之外,所有查询参数都以&
sessionBrand
。 What you need in a URL is for the first parameter to start with a ?
URL中需要的是第一个参数以
?
开头?
, and all others with a &
. 以及所有其他带有
&
。 As you saw, your code doesn't do this when there is no sessionBrand
. 如您所见,当没有
sessionBrand
时,您的代码不会执行此sessionBrand
。
There are number of ways to fix this. 有很多方法可以解决此问题。 Probably the neatest I can think of is to assemble the various parts as you are, but without any prefixes - then join them all together at the end.
我可能想到的最整洁的方法是,按原样组装各个部分,但没有任何前缀-然后将它们全部结合在一起。 Like this (I just saw Viktor's solution, it's exactly the same idea, but neater because he rewrote more of your earlier code):
像这样(我刚刚看过Viktor的解决方案,这是完全相同的想法,但是更整洁,因为他重写了更多的先前代码):
if(sessionBrand)
var brand = 'brand='+sessionBrand;
else
var brand = '';
if(sessionModel)
var model = 'model='+sessionModel;
else
var model = '';
if(sessionCategory)
var category = 'category='+sessionCategory;
else
var category = '';
if(sessionKeyword)
var keyword = 'keyword='+this.sessionKeyword;
else
var keyword = '';
var queryString = '?' + [sessionBrand, sessionModel, sessionCategory, sessionKeyword].filter(function(str) {
return str.length > 0;
}).join("&");
var newUrl = url+queryString;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.