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

I need to check if this URI has the success parameter. How can I do this preferably with javascript. The success parameter is optional.

pseudo code

if has success parameter in URI:
   do A

else:
   do B
fejese
  • 4,601
  • 4
  • 29
  • 36
user2983258
  • 151
  • 1
  • 3
  • 9

3 Answers3

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

if(a >= 0){
  // means success exists
}else{
 // means success don't exists
}
Ankit Tyagi
  • 2,381
  • 10
  • 19
0

You can use a regex of /\/success(\/|$)/ to test this:

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

This will accept /success/ anywhere in the string and would match:

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

If you only want to match success and the end of the URL, you should use /\/success\/?$/:

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

Which will only match:

http://xxxxxxxxx:8000/compare/387552/1/389688/1/success
http://xxxxxxxxx:8000/compare/387552/1/389688/1/success/
h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
0

Try the Javascript function indexof

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

That's all .

Madurai
  • 297
  • 3
  • 16