Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery check current url

Tags:

jquery

url

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.

like image 966
James Avatar asked Sep 20 '10 16:09

James


2 Answers

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
}
like image 178
Jacob Relkin Avatar answered Sep 24 '22 03:09

Jacob Relkin


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'));
like image 34
Josh Stodola Avatar answered Sep 26 '22 03:09

Josh Stodola