我如何告诉 jQuery 只在一个特定的网页上运行一个函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3087206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I tell jQuery to run a function on one specific web page only?
提问by Joseph
I have some jQuery code that is on every web page of the site (in the head element).
我在网站的每个网页上都有一些 jQuery 代码(在 head 元素中)。
What selector syntax can I use to tell jQuery that a certain function should only run on one page and not all the other pages on the site?
我可以使用什么选择器语法来告诉 jQuery 某个函数应该只在一个页面上运行,而不能在站点上的所有其他页面上运行?
Can I specify a page name or URL in the selector somehow?
我可以以某种方式在选择器中指定页面名称或 URL 吗?
Many Thanks
非常感谢
回答by Denis 'Alpheus' Cahuk
You could use an if statement checking for top.location.pathname.
您可以使用 if 语句检查 top.location.pathname。
if (top.location.pathname === '/my/path')
{
/* magic ... */
}
Or if you want to make it more portable and give the actual if statement some meaning (so someone reading it will know what it's about) - if you have access to the body element of the document, you can add a class showing that you want to run this script.
或者,如果你想让它更便携,并赋予实际的 if 语句一些意义(这样有人阅读它就会知道它是关于什么的) - 如果你可以访问文档的 body 元素,你可以添加一个显示你想要的类运行这个脚本。
So for instance, if /* magic ... */
in the above example has something to do with including the Facebook API, you could make your body look like <body class="has-facebook-api">
and then make a check with jQuery:
因此,例如,如果/* magic ... */
在上面的示例中与包含 Facebook API 有关,您可以使您的身体看起来像<body class="has-facebook-api">
,然后使用 jQuery 进行检查:
$(function () // on document.ready()
{
if ($('body.has-facebook-api').length > 0)
{
/* magic ... */
}
});
Make sure this runs after including jQuery inside a separate script tag.
确保在单独的脚本标签中包含 jQuery 后运行。
While we're at it, if you're not using this script to transform the visuals of your page before it gets outputted, I'd advise you to place all or most of your script tags close to your footer to render the page sooner.
虽然我们正在这样做,但如果您在输出页面之前不使用此脚本来转换页面的视觉效果,我建议您将所有或大部分脚本标签放置在靠近页脚的位置以更快地呈现页面.
回答by Kyprus
Maybe check window.location.pathname
?
也许检查window.location.pathname
?
if (window.location.pathname == "/path/to/page.html") {
callFunction();
}
回答by gnarf
You can't specify the current URL in a jQuery Selector, you can however write a simple check using the document.location
object:
您不能在 jQuery 选择器中指定当前 URL,但是您可以使用该document.location
对象编写一个简单的检查:
if (document.location.pathname == "/somefolder/somepage") {
// do some special stuff on this page!
}
回答by Nathan Hess
I prefer to do stuff like this in whatever language you are writing your app in. Your layout/template/whatever that is serving up your html could simply output a true/false to a javascript variable
我更喜欢用你编写应用程序的任何语言来做这样的事情。你的布局/模板/任何提供你的 html 的东西都可以简单地将一个真/假输出到一个 javascript 变量
if ($page = '/foo/bar.html/') {
echo 'baz = true';
} else {
echo 'baz = false';
}
Then have your js check baz to determine whether you should be running these functions or not. You aren't relying 100% on js in this instance, but imo that's better. Your server is going to always know what page/url you are on, and you won't need to worry about browser incompatibilities with your js trying to determine what page it resides on.
然后让你的 js 检查 baz 来确定你是否应该运行这些函数。在这种情况下,您不是 100% 依赖 js,但 imo 更好。您的服务器将始终知道您所在的页面/网址,并且您无需担心浏览器与您的 js 不兼容,试图确定它驻留在哪个页面上。