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 7038265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:38:53+00:00 2026-05-28T01:38:53+00:00

In my app I have jquery that is specific to each page, that is

  • 0

In my app I have jquery that is specific to each page, that is scripts that are used strictly on one page. Is there a way to set a scope that I can work within to avoid any id/class collisions or behaviour creeping from one page to another?

I could do this, for example by specifying the wrapper div for the specific page

$(function(){
  $('#thing_page input.finder').focus(function(){ ... })
});

but I would need to prepend all the event bindings with the name of the page wrapper, which is a bit annoying.

For those who don’t know, rails compresses all the javascript files in to one, which I like. Otherwise I could just include each file on a specific page.

  • 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-28T01:38:54+00:00Added an answer on May 28, 2026 at 1:38 am

    You can wrap it all in a giant switch statements. Assuming thing_page is the id for the body, you could do this:

    $(function() {
        switch (document.body.id) {
            case 'thing_page':
               // Do stuff for thing page
               $('input.finder').focus(function(){ ... });
               break;
            case 'other_page':
               // do stuff for other page
               break;
            case 'third_page':
               // Do stuff for third page
               break;
            // ...and so on and so forth...
        }
    });
    

    The syntax highlighting here is pretty bad. Take a look at it in jsfiddle: http://jsfiddle.net/dFuVz/


    Alternatively, you might consider using an object to contain your functions (similar to @Andrew’s approach):

    var pageFunctions = {
        thing_page: function() {
            // Code for thing_page
            $('#thing_page input.finder').focus(function(){...});
        },
        other_page: function() {
            // Code for other_page
        },
        third_page: function() {
            // Code for third_page
        }
    };
    
    // Now call the correct function, according to the body ID
    pageFunctions[ document.body.id ]();
    

    Again, since the syntax highlighting here is so terrible, look at it here: http://jsfiddle.net/9L592/

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

Sidebar

Related Questions

I have some simple jQuery that renders a page in my Rails app. Here's
I have a web app that I've recently applied a jQuery ThemeRoller theme to.
I have an MVC app that does a lot of work with jQuery ajax
I have an HTML (App) file that reads another HTML (data) file via jQuery.ajax()
I have a MVC app and using JQuery. I have anchor that I setup
I have a web app (JSPs and Servlets along with jQuery) that I am
I have a single page jQuery Mobile app with four data-role='pages' in place; so,
I am working on a mobile app and I have an initial page that
I have a set of PHP server side scripts that maintain user session state
I have a mobile app created using HTML/JS(jQuery)/CSS and I'm looking to include page

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.