我们在页面 'http://site.com/movies/moviename/'
如何知道当前URL(直接位于网站根目录后面)中是否存在/movies/
?
代码应该返回:
对于 'http://site.com/movies/moviename/'
,应该返回True
对于 'http://site.com/person/brad-pitt/movies/'
,应该返回False
谢谢。
String
对象的 indexOf
方法:var url = window.location.href;
var host = window.location.host;
if(url.indexOf('http://' + host + '/movies') != -1) {
//match
}
在jQuery中没有你需要用到的东西,这是纯JavaScript。
if (/\/\/[^\/]+\/movies\//.test(window.location.href)) {
// inside the movies folder
}
基本字符串操作...
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'));