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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 271k
  • Answers 271k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Have a look at at OpenCV's SURF feature extraction (they… May 13, 2026 at 1:45 pm
  • Editorial Team
    Editorial Team added an answer You can use the keydown event listener to listen for… May 13, 2026 at 1:45 pm
  • Editorial Team
    Editorial Team added an answer Use profiling tools, such as YourKit, JProfiler and HPROF (this… May 13, 2026 at 1:45 pm

Related Questions

We're maintaining a web product that we've sold to several different customers. We support
I'd like to ask your input on what the best practice is for handling
This is my problem: I need to store a lot of log messages and
This is my situation: I have two languages, namely English and German. However, I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.