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

  • SEARCH
  • Home
  • 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 7766895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:32:21+00:00 2026-06-01T15:32:21+00:00

Just wandering if it makes sense, to use if statements like below within jquery

  • 0

Just wandering if it makes sense, to use if statements like below within jquery document ready function to ensure script is only fired if on a specific page..

 if(window.location.href.indexOf("our-team")>= 0){
            if(window.location.hash) {
                var hash = window.location.hash;
                $('#teamMembers div:not('+hash+')').hide();
                $("#teamMenu .imgHolder").removeClass('active');
                $('a[href='+hash+']').closest(".imgHolder").addClass('active');
            }
            else {
                $('#teamMembers div:not(#member1)').hide();
            }
        }

This is probably not very efficient, but is the if statement necessary to stop the script running if the current page is not containing our-team in the url.

Any ideas guys?

  • 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-06-01T15:32:22+00:00Added an answer on June 1, 2026 at 3:32 pm

    You will obtain the best performance if you don’t run a lot of selector operations on pages that those items don’t exist on. So, if it’s easy to filter out those selector operations by checking the URL or some other quick check, then that may be desirable for performance reasons.

    Personally, I prefer to identify page types with a class name on the body tag rather than examining the URL. That means you’re looking more for the expected content rather than where the content came from and if someone managing the site decides to change the URL layout or names, your code won’t be dependent upon that.

    if ($(document.body).hasClass("ourteam")) {
        if (window.location.hash) {
            var hash = window.location.hash;
            $('#teamMembers div:not('+hash+')').hide();
            $("#teamMenu .imgHolder").removeClass('active');
            $('a[href='+hash+']').closest(".imgHolder").addClass('active');
        } else {
            $('#teamMembers div:not(#member1)').hide();
        }
    }
    

    Technically speaking, the first if statement is not required if none of the selectors you are using exist in pages that you don’t want to process. But running 4-5 extra selector operations on lots of pages that they don’t apply to is not efficient. And, if every page has this type of code in the common JS and thus it’s 4-5 operations * 50 pages, then that would be a lot of wasted selector operations and could really affect site performance. The more pages you have, the more chance you may repeat a selector in another page and find the JS from one page is accidentally impacting another page too.

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

Sidebar

Related Questions

Is this bad practice to use php statements such as 'if' within a jquery
I've just started to use jquery and asp.net, bearing this in mind I was
Hey, I'm just wondering if there's any easy function to make text suitable to
Just wandering if its a good idea to put username and password in the
Is TeeChart .NET built from the same source as TeeChart VCL? I'm just wandering
Just wondering why people like case sensitivity in a programming language? I'm not trying
Just wondering if this is considered a clear use of goto in C#: IDatabase
Does it make sense to use Qt for increasing the productivity in an MFC
Hello I've just implemented a search option on my application use this on the
I'm just wondering if it is semantically correct to use traits to build game

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.