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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:34:32+00:00 2026-05-15T16:34:32+00:00

I am using jQuery and I am loading dynamic HTML via PHP. My question

  • 0

I am using jQuery and I am loading dynamic HTML via PHP. My question is, what is the best practice concerning elements/attributes that may or may not exist on any given page? Should I have one .js file containing all the possibilities (I would think this is inefficient) or should I use a bunch of script tags (that seems also inefficient)? Is there a “lazy” initialization way to do this?

For instance, say I have the following:

<div id="list">
   <ul>
      <li>some</li>
      <li>stuff</li>
      <li>here</li>
   </ul>
</div>

But this list is generated only if necessary, not on all pages. Should my global js file have the following:

$("#list").click( function(){ 
   $(this).removeClass('collapse').addClass('expand'); 
});

Or should I add it in a script tag just above the div that is being generated at the bottom of the page after creating the div is output via PHP?

Thanks SO, you guys are awesome. Can’t wait until I know enough to start answering some questions!

Edit, just thought of a quick way to ask. Which is better, jQuery running with a bunch of selectors that don’t match or passing javascript in script tags via PHP?

  • 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-15T16:34:33+00:00Added an answer on May 15, 2026 at 4:34 pm

    2 Things:

    Loading 1 big js file is faster than loading many small ones, as it avoids the overhead of many connections

    If your can tell clients to cache the js then it will only be loaded on the first visit.

    Also you say add it to the HTML just above the tag. When a web browser encounters JS it stops rendering until the JS is loaded and executed. So you want to put the JS at the bottom of the page, so your page appears to the user as soon as possible.

    Not that I’m saying 1 big file is the way to go mind, it depends on many things. These are just things to bear in mind.

    EDIT TO ADD: But I would definitely put it in external js and not HTML, so you can get client side caching working for you. And call those JS from the bottom of your page so as not to block rendering. Sorry, this is a bit of a messy answer but this is a big topic, there are many factors to consider and no right answer.

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

Sidebar

Related Questions

Using jQuery 1.7.1, I am loading some HTML fragments via AJAX that are injected
I'm using jQuery UI tabs, loading the tab content via ajax. Content may contain
First of all In Jquery loading html frag with dynamic content using load() is
I have some content that I am loading using jquery ajax. The content has
I'm loading form into jquery-ui dialog using jquery.html() function and then Submitting doesn't work
After loading a PHP template (using jQuery's load function), this simple script won't make
I'm using jQuery version 1.7.1 from Google CDN and have the following code: $(menuInstance).delay(2000).removeClass('loading').html(ul);
I want to show page loading spinner in my html page content using jquery
i'm loading, using the JQuery ajax() method, an external page with both html and
I'm loading reCAPTCHA via JavaScript http://www.google.com/recaptcha/api/js/recaptcha_ajax.js and am using jquery-1.5.2.min.js to communicate with a

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.