-1

I want to find out, how can i see if there's any character/string after a specific string part from a string. My question sounds ambiguous but here's my real example:

I can have 2 urls : http://domain/classAdd?itemId=1123123 or http://domain/classAdd?ItemId=

I want to put a condition in HTML regarding the url, to have specific header on my site. How can i check if the url has a itemId or not?

At moment i'm getting only :location.indexOf('classAdd?itemId=') == -1 I tried to verify the length but doesn't work. I searched a lot but i didn't find anything to answer my question, only how to get Specific string from a string. My Id is dynamic and the substring won't work

Thank you guys.

TrulyXax
  • 653
  • 2
  • 7
  • 21

2 Answers2

2

check if this

window.location.href.substring(window.location.href.indexOf('classAdd?itemId=') + 16)

gives you the itemId

Aleksandar Matic
  • 779
  • 1
  • 9
  • 21
  • Can i know why +16 ? – TrulyXax Sep 23 '16 at 13:36
  • Are talking about guid's length? – TrulyXax Sep 23 '16 at 13:37
  • That's the length of 'classAdd?itemId='. indexOf get's you the starting position of the substring and you neded to add it's length. Please check my answer edit. – Aleksandar Matic Sep 23 '16 at 13:39
  • yes, the code works, but i will have a dynamic domain and dynamic page 'classAdd' – TrulyXax Sep 23 '16 at 13:42
  • i need to check if there's anything after "itemId=" part – TrulyXax Sep 23 '16 at 13:42
  • window.location.href.substring(window.location.href.indexOf('itemId=') + 7) – Aleksandar Matic Sep 23 '16 at 13:43
  • `window.location.href.substring(window.location.href.indexOf('itemId=') + 7)` works well, but how can i see if there's something after itemId, i don't need it's value. I've got this line into an If statement into a `ng-show', and if there's something after itemId= i'll include an html , if not i'll include another html – TrulyXax Sep 23 '16 at 13:48
  • just check if window.location.href.substring(window.location.href.indexOf(‌​'itemId=') + 7) != '' I don't use Angular, but I'm sure you can squeeze this in. – Aleksandar Matic Sep 23 '16 at 13:52
0
function(){
    var indexOfEqual = window.location.href.indexOf("=");
    if(indexOfEqual < window.location.href.length) return true;
    return false;
}
DrEarnest
  • 808
  • 2
  • 12
  • 27
  • I think this is too general. What if there was e.g. OrderId= instead of ItemId=, it would still return true when really it should return false in TrulyXax's case. – Aleksandar Matic Sep 23 '16 at 13:49