This checks "if we are on movies.php
page":
if (location.href.match(/movies.php/)) {
// something happens
}
how to add for this (like or
) "if we are on music.php
page"?
This checks "if we are on movies.php
page":
if (location.href.match(/movies.php/)) {
// something happens
}
how to add for this (like or
) "if we are on music.php
page"?
I assume you mean you want to see if you are on movies.php
or on music.php
? Meaning you want to do the same thing if you are on either?
if (location.href.match(/movies\.php/) || location.href.match(/music\.php/)) {
// something happens
}
Or if you want to do something different, you can use an else if
if (location.href.match(/movies\.php/)) {
// something happens
}
else if(location.href.match(/music\.php/)) {
// something else happens
}
Also, instead of using match
you can use test
:
if (/movies\.php/.test(location.href) || /music\.php/.test(location.href)) {
// something happens
}
Based on paulj's answer, you can refine the regular expressions in if
statement that checks to see if you are on either page, to a single regular expression:
/(music|movies)\.php/
How about ..
if (/(movies\.php|music\.php)/.test(location.href)) {
// Do something
}
Or even better...
if (/(movies|music)\.php/).test(location.href)) {
// Do something
}
Note the \., this literally matches "a single period" where as in regex . matches any character, thus these are true, but probably not what you want...
if (/movies.php/.test('movies_php')) alert(0);
if (/movies.php/.test('movies/php')) alert(0);
The following improves on previous answers in a couple ways ...
if (/(movies|music)\.php$/.test(location.pathname)) {
var pageName = RegExp.$1; // Will be either 'music' or 'movies'
}