Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 878871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:50:05+00:00 2026-05-15T11:50:05+00:00

I have some jQuery code that is on every web page of the site

  • 0

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 run on one page and not all the other pages on the site?

Can I specify a page name or URL in the selector somehow?

Many Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T11:50:05+00:00Added an answer on May 15, 2026 at 11:50 am

    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 <body class="has-facebook-api"> 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some jQuery code that intercepts links clicked on a page: $(document).ready(function() {
I have some jQuery code that runs on every $(document).ready() event. I also have
I have some jquery code that is doing an ajax lookup and returning comma
I have some jQuery code that highlights a link when clicked, and changes the
I have some jQuery/JavaScript code that I want to run only when there is
I have some jQuery code. I have called an Ajax function file, file.php, that
Trying to get comfortable with jQuery and I have encountered some sample code that
I have a page that uses jQuery's $.post() method to get some html and
I have a web app. that uses AJAX (via jQuery). I noticed that some
I have some code in jQuery that iterate through children in div using each().

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.