check is参数在URI中可用

时间:2013-12-06 11:26:50

标签: javascript jquery-plugins

http://xxxxxxxxx:8000/compare/387552/1/389688/1/success

我需要检查此URI是否具有success参数。我怎么能用javascript做这个。 success参数是可选的。

伪代码

if has success parameter in URI:
   do A

else:
   do B

3 个答案:

答案 0 :(得分:1)

var str = window.location.href;
var a = str.indexOf('success');

if(a >= 0){
  // means success exists
}else{
 // means success don't exists
}

答案 1 :(得分:0)

您可以使用/\/success(\/|$)/的正则表达式对此进行测试:

if (/\/success(\/|$)/.test(window.location.href)) {
    console.log('Yep, it has success');
} else {
    console.log('Nope - no success');
}

这将接受字符串中任何位置的/success/并匹配:

http://xxxxxxxxx:8000/compare/387552/1/389688/1/success
http://xxxxxxxxx:8000/compare/387552/1/389688/success/1
http://xxxxxxxxx:8000/compare/387552/1/success/389688/1
http://xxxxxxxxx:8000/compare/success/387552/1/389688/1
http://xxxxxxxxx:8000/success/compare/387552/1/389688/1

如果您只想匹配网址的成功与结束,则应使用/\/success\/?$/

if (/\/success\/?$/.test(window.location.href)) {
    console.log('Yep, it has success');
} else {
    console.log('Nope - no success');
}

仅匹配:

http://xxxxxxxxx:8000/compare/387552/1/389688/1/success
http://xxxxxxxxx:8000/compare/387552/1/389688/1/success/

答案 2 :(得分:0)

尝试使用Javascript函数 indexof

var url="http://xxxxxxxxx:8000/compare/387552/1/389688/1/success";
if(url.indexOf("success") > -1== true)
{
alert("present");
}
else
{
alert("not present");
}

就是这样。