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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:38:42+00:00 2026-06-14T15:38:42+00:00

Maybe it is a silly question but I am a very confused with JavaScript.

  • 0

Maybe it is a silly question but I am a very confused with JavaScript. I follow this code organisation with as much as possible unobtrusive JavaScript. I build a website in Ruby on Rails that uses Google Maps.

My scenario: Suppose I have a REALLY enormous website in which, in different pages, I need to initiate JavaScript scripts which do some actions.

A problem is that I have to add ALL the JavaScript code of my website to the user’s browser regardless of which page the user visits. For instance, let’s say I have page A which uses enormous JavaScript code and page B which uses some tiny JavaScript code. As far as I understand, in page B the user will have to retrieve ALL the JavaScript code (that is, for both pages A and B). Isn’t that a little impractical?

What is more, for both pages, JavaScript is initiated by document.addEventListener("DOMContentLoaded", function, false); function. However, the function of page A supposes that there is a div named “pageA” (which uses as a reference in order to modify it or add elements through js scripts) and similarly the function of page B supposes that there is a div named “pageB”. Hence, when I am on page B I receive js error that there is no element of “pageA”. How can I avoid that ?

Given the fact that, for every single page I have to load the user with my whole JavaScript code (that is for all pages) how can I initiate(that is, to start immediately when page loads and not after a user event happens) page specific JavaScript code without any such errors? I am really confused!

Wooho very fast responses thanx guys! But I never said I write the whole js code in 1 single file. Instead I uses RoR’s organisation! (which btw combines all files into one single file). To clarify my problem, in different files I initiate events for page A and events for page B (or C etc..). The code fires as soon as DOMContentLoaded event happens. However when the user is on page B I don’t want to fire the events for page A. Or similarly, I want to load only js for page B (so that no event for page A fires). How can I do that?

PS: If you think a better title, feel free to suggest it!

  • 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-06-14T15:38:43+00:00Added an answer on June 14, 2026 at 3:38 pm

    If you keep your Javascript code in a single file it only has to be loaded once (and perhaps even compiled once, see this), when a user requests their first page of your website. Then the browser will use a cached copy unless you disallow caching (bad configuration of your web server can do that).

    When you do something like (JQuery):

    $('#nosuchid').something();
    

    or e.g.

    $('.nosuchclass').each(function(i, el) { ... });
    

    there will be no error and no effect if no matching elements exist in your markup. If you use, say, document.getElementById(), it will return null if no elements match and you will have to handle that case. If you do that properly there aren’t going to be any errors.

    As you can imagine, the code which is skipped doesn’t take time to execute, so there’s no reason to expect performance problems, either.

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

Sidebar

Related Questions

It this possible? How? (Maybe silly question, but I couldn't find answer :( )
Maybe this is a very silly question, but the book I'm reading instructed me
Maybe this is a very silly question but I'm new using django. I have
Ok so this maybe a simple/silly question but I don't know so here goes:
Maybe this is a silly question, but I'm working on a project that wants
My question may be very silly but here it is. I went through this
Maybe this question is kind of silly but I have no clue how to
Maybe a silly question, but where do I fill in this URL? I have
Maybe this is a silly question, but today I was working in my hosts
This may be a silly question, but googling that is not very effective. And

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.