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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:05:05+00:00 2026-05-11T02:05:05+00:00

What is the best way to handle several different onload scripts spread across many

  • 0

What is the best way to handle several different onload scripts spread across many pages?

For example, I have 50 different pages, and on each page I want to set a different button click handler when the dom is ready.

Is it best to set onclicks like this on each individual page,

 <a id='link1' href='#' onclick='myFunc()' /> 

Or a very long document ready function in an external js file,

 Element.observe(window, 'load', function() {   if ($('link1')) {     // set click handler   }   if ($('link2')) {    // set click hanlder   }   ... } 

Or split each if ($('link')) {} section into script tags and place them on appropriate pages,

Or lastly, split each if ($('link')) {} section into its own separate js file and load appropriately per page?

Solution 1 seems like the least elegant and is relatively obtrusive, solution 2 will lead to a very lengthy load function, solution 3 is less obtrusive then 1 but still not great, and solution 4 will load require the user to download a separate js file per page he visits.

Are any of these best (or worst) or is there a solution 5 I’m not thinking of?

Edit: I am asking about the design pattern, not which onload function is the proper one to use.

  • 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. 2026-05-11T02:05:05+00:00Added an answer on May 11, 2026 at 2:05 am

    Have you thought about making a class for each type of behavior you’d like to attach to an element? That way you could reuse functionality between pages, just in case there was overlap.

    For example, let’s say that on some of the pages you want to a have button that pops up some extra information on the page. Your html could look like this:

    <a href='#' class='more-info'>More info</a> 

    And your JavaScript could look like this:

    jQuery('.more-info').click(function() { ... }); 

    If you stuck to some kind of convention, you could also add multiple classes to a link and have it do a few different things if you needed (since jQuery will let you stack event handlers on an element).

    Basically, you’re focusing on the behaviors for each type of element you’re attaching JavaScript to, rather than picking out specific ids of elements to attach functionality to.

    I’d also suggest putting all of the JavaScript into one common file or a limited number of common files. The main reason being that, after the first page load, the JavaScript would be cached and won’t need to load on each page. Another reason is that it would encourage you do develop common behaviors for buttons that are available throughout the site.

    In any case, I would discourage attaching the onlick directly in the html (option #1). It’s obtrusive and limits the flexibility you have with your JavaScript.

    Edit: I didn’t realize Diodeus had posted a very similar answer (which I agree with).

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

Sidebar

Related Questions

I have a product entity which has several classes (each product type has different
The question is as follows : what's the best way to handle different database
What's the best way to handle translations for stock text in a SSRS .
What is the best way to handle a big team that has access to
What's the best way to handle a visitor constructing their own URL and replacing
Is this the best way to handle file moving in a windows service? we
What's the best way to handle data entered through dynamically generated controls in ASP.NET?
What is the best way to handle unexisting actions in the Zend Framework? Depending
What's the best way to handle an expired password in an ASP.NET MVC application?
What is the best way to handle multiple Flex/Bison parsers inside a project? I

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.