I have some jQuery code that is on every web page of the site (in the head element).
What selector syntax can I use to tell jQuery that a certain function should only ru
You could use an if statement checking for 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.
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 and then make a check with 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.
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.