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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:33:04+00:00 2026-05-27T04:33:04+00:00

I’m looking for a better way to manage global component/plugin/widget initialization in a large

  • 0

I’m looking for a better way to manage global component/plugin/widget initialization in a large project. It has many jQuery-powered components that I would like to initialize quickly & efficiently and after scouring the internet, I’ve only really found short-sighted examples that are only realistic/performant in smaller sites.

The Problem

I Want to find a smart & elegant way of getting rid of this:

$(function() { $('.widget-one').widgetOne(); });
$(function() { $('.widget-two').widgetTwo(); });
$(function() { $('.widget-three').widgetThree(); });
$(function() { $('.widget-four').widgetFour(); });

Now before you smack me for it, let me state that I know that in most cases (but not all) .widget-one is a bad selector as it will on older browsers get all elements in the dom and check for the class.

The issue is, these widgets are not one-off’s, and I may not know about thier existance ahead of time (generated in a web app view, maybe 2-3 times based on logic or a loop of products or something).

So the following solutions are out:

$(function() { $('#WidgetOne').widgetOne(); });

and

<span id="WidgetOne_12345">...</span>
<script type="javascript">
   $(function() { $('#WidgetOne_12345').widgetOne(); });
</script>

Thoughts

This is not a new problem, It’s been around since day one. It still baffles me how this is so difficult to solve with jQuery even at this level of maturity. Either that or I’m missing something blatantly obvious.

Unfortunately google-fu turns out badly on this subject as everyone suggests one of two things:

  • jQuery’s .live() or .delegate() catch-all event handlers. This is just scary on a fundamental level. .delegate() wouldn’t be so bad, but this requires that the plugin/widget/control/whatever be entirely event driven. This would work in a lot of cases for sure, but not in others. It also makes following & organizing the code very complex. I won’t even go into .live(), for large complex sites, the event bubbling is slow & when you get enough components together, the list of queries to match becomes large making each click/focus/whatever event incrementally slower on the whole for the entire page.
  • Work arounds that involve plugins like liveQuery, which is a very cool plugin for sure but it primarily seems to have been designed to solve a different issue (the issue of new items being brought in by ajax/dom creation), and still will get incrementally slower the more queries that stack up that need to be checked against.

Conclusion

There has to be a better way, I know there has to be one. I’ve exhausted my google fu on the subject, and still cannot find ideas/concepts/examples/discussion that is newer than jQuery 1.3.2 or with a grand picture in mind. In a perfect world this wouldn’t be a problem because everyone would use intelligent browsers with modern standards & a decent javascript engine, and .class queries wouldn’t take eons and eons, but unfortunately this is not the case.

I’m looking for ideas on how to tackle this, after going through many SO questions similar to this, and many articles on various jquery techniques I feel that if the information is out there it’s buried underneath the many false positives & “101 best jquery plugin” results that turn up in any search with jQuery. I know someone out there has ran into this predicament.

Ideas, links, examples, anything is welcome, there just has to be a better way.

  • 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-27T04:33:04+00:00Added an answer on May 27, 2026 at 4:33 am

    just brainstorming off the top of my head I can think of one thing that would decrease your “select and initialize” time, but may be a trade off in pain during development. furthermore, you can’t really use params with this method, but if you have a situation exactly like the one you describe above, this may be worth doing.

    if you maintain that each of your initializing elements all have the same class (like widget for example), you can create a single selector system of initializing your widgets.

    structure the classes instead like this in your HTML:

    <div class="widget" widgetFunc="widgetOne"></div>
    <div class="widget" widgetFunc="widgetTwo"></div>
    

    etc.

    and your jquery init function will look like:

    var $widgetInitializerElements = $('.widget');
    
    $.each($widgetInitializerElements,function(){ 
        var initialize = window[$(this).attr('widgetFunc')];
    
        if(typeof initialize === 'function'){
            $(this).initialize(); 
        }
    });
    

    so this would reduce your selection to one time, and keep your jquery pretty clean. you just have to worry about all those HTML elements which may not be feasible and you will have trouble if you need to add params to initialize calls :/

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I want to count how many characters a certain string has in PHP, but
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I would like to count the length of a string with PHP. The string

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.