I have the following code to trim an URL to check if a parameter exists. For example if the URL is http://localhost:3000/page?name=jack?number=2000
, then the code should have subString
as 2000
. There could be cases where the number parameter does not exist in the URL.
var url = window.location.href;
var subString = url.substring(url.lastIndexOf('number=') + 7);
alert(subString);
alert(subString.length);
if (subString.length <= 0) {
//logic here
}
This is not working as expected. For whatever reason, when the number parameter is not in the URL, subString becomes the full URL. However, when there is a number, then it works fine. How do I fix this?