We are on page 'http://site.com/movies/moviename/'
How can we know, is there /movies/ in current url (directly after site's root)?
Code should give:
True for 'http://site.com/movies/moviename/'
And false for 'http://site.com/person/brad-pitt/movies/'
Thanks.
You can try the String object's indexOf method:
var url = window.location.href;
var host = window.location.host;
if(url.indexOf('http://' + host + '/movies') != -1) {
   //match
}
                        Basic string manipulation...
function isValidPath(str, path) {
  str = str.substring(str.indexOf('://') + 3);
  str = str.substring(str.indexOf('/') + 1);
  return (str.indexOf(path) == 0);
}
var url = 'http://site.com/movies/moviename/'; // Use location.href for current
alert(isValidPath(url, 'movies'));
url = 'http://site.com/person/brad-pitt/movies/';
alert(isValidPath(url, 'movies'));
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With