JavaScript 位置

发布于 2024-09-05 11:18:19 字数 225 浏览 6 评论 0原文

这会检查“如果我们在 movies.php 页面上”:

if (location.href.match(/movies.php/)) {
// something happens
}

如何为此添加(如)“如果我们在 >music.php 页面”?

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"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

煮酒 2024-09-12 11:18:19

我假设您的意思是您想查看您是在 movies.php 还是 music.php 上?这意味着如果您选择其中任何一个,您也想做同样的事情吗?

if (location.href.match(/movies\.php/) || location.href.match(/music\.php/)) {
// something happens
}

或者,如果您想做一些不同的事情,您可以使用 else if

if (location.href.match(/movies\.php/)) {
// something happens
}

else if(location.href.match(/music\.php/)) {
// something else happens
}

另外,您可以使用 test 而不是使用 match

if (/movies\.php/.test(location.href) || /music\.php/.test(location.href)) {
// something happens
}

基于 paulj's答案,您可以将 if 语句中的正则表达式(检查您是否在任一页面上)细化为单个正则表达式:

/(music|movies)\.php/

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/
熊抱啵儿 2024-09-12 11:18:19

怎么样..

if (/(movies\.php|music\.php)/.test(location.href)) {
// Do something
}

或者甚至更好...

if (/(movies|music)\.php/).test(location.href)) {
// Do something
}

注意 \.,这字面意思是匹配“单个句点”,如 regex 中的那样。匹配任何字符,因此这些是正确的,但可能不是你想要的......

if (/movies.php/.test('movies_php')) alert(0);
if (/movies.php/.test('movies/php')) alert(0);

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);
泅人 2024-09-12 11:18:19

以下以多种方式改进了以前的答案...

if (/(movies|music)\.php$/.test(location.pathname)) {
    var pageName = RegExp.$1; // Will be either 'music' or 'movies'
}
  • 通过 RegExp.$1 属性提供页面名称(无 .php 扩展名)
  • 使用 location.pathname 消除了对可能的查询参数的无关命中(例如“...?redirect”) =music.php")
  • 使用正则表达式 '|'运算符将测试组合到单个正则表达式中(如果您有很多要匹配的页面,则特别好)
  • 使用正则表达式“$”运算符将匹配限制到路径名的末尾(避免路径中间的无关命中。在您的情况下不太可能示例,但很好的做法)

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'
}
  • Provides the name of the page (sans .php extension) via the RegExp.$1 property
  • Using location.pathname eliminates extraneous hits on possible query parameters (e.g. "...?redirect=music.php")
  • Use of regex '|' operator combines tests into a single regex (particularly good if you have lots of pages you want to match)
  • Use of regex '$' operator constrains match to end of pathname (avoids extraneous hits in the middle of a path. Not very likely in your example, but good practice)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文