Find string in URL - joomla/jQuery
I've got a "simple" problem that I can't get my head around, I want to search my URL, and find either 开发者_StackOverflow中文版/nl/
or /en/
or /fr/
and add a class to the body accordingly.
I've searched high and low and can't seem to find the right answer.
The URL is build "cleanly', so it reads: www.mysite.com/nl/mypage
.
You could try something like -
if (location.href.indexOf('/en/') > -1) {
$("body").addClass("enClass");
}
Use regular expressions. Capture the URL as a variable:
var url = window.location;
Then search for the languages:
var pattern = /\/en\/|\/nl\/|\/fr\//
if (url.match(pattern)) {
// there's a match
} else {
// there isn't
}
If you want to do an if
statement for just one language, change the pattern to only include that one language. For example, en
:
var pattern = /\/en\//;
if (url.match(pattern)) {
// on en
} else {
// not on en
}
Try this:
$url = 'www.mysite.com/nl/mypage...';
$url_parts = explode('/', $url);
foreach($url_parts as $part):
if($part = 'nl'){ var_dump($part); }
endforeach;
精彩评论