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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:00:50+00:00 2026-06-10T08:00:50+00:00

i am currently learning about javascript namespaces as i build a website and i

  • 0

i am currently learning about javascript namespaces as i build a website and i have the following requirements: i want to make all of my code private so that other public scripts on the page (possibly adverts, i’m not too sure at this stage) cannot override or alter my javascript. the problem i am foreseeing is that the public scripts may use window.onload and i do not want them to override my private version of window.onload. i do still want to let them run window.onload though.

so far i have the following layout:

//public code not written by me - i'm thinking this will be executed first
window.onload = function() {
    document.getElementById('pub').onclick = function() {
        alert('ran a public event');
    };
};

//private code written by me
(function() {
    var public_onload = window.onload; //save the public for later use
    window.onload = function() {
        document.getElementById('priv').onclick = function() {
            a = a + 1
            alert('ran a private event. a is ' + a);
        };
    };
    if(public_onload) public_onload();
    var a = 1;
})();

i have quite a few questions about this…

firstly, is this a good structure for writing my javascript code, or is there a better one? (i’m planning on putting all of my code within the anonymous function). is my private code really private, or is there a way that the public javascript can access it? i’m guessing the answer to this is “yes – using tricky eval techniques. do not embed code you do not trust”, but i’d like to know how this would be done if so.

secondly, when i click on the public link, the event is not fired. why is this?

finally, if i comment out the if(public_onload) public_onload(); line then a is returned correctly when i click the private button. but if i leave this line in then a‘s value is nan. why is this?

  • 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-10T08:00:51+00:00Added an answer on June 10, 2026 at 8:00 am

    You can attach event listeners to avoid their overriding in some way like this:

    <ol id="res"></ol>
    
    <script type="text/javascript">
        var res = document.getElementById('res');
    
        function log(line) {
            var li = document.createElement('li');
            li.innerHTML = line;
            res.appendChild(li);
        }
    
        // global code:
        window.onload = function() {
            log('inside the global window.onload handler');
        };
    
        // private code:
        (function(window) {
            function addEvent(el, ev, fn) {
                if (el.addEventListener) {
                    el.addEventListener(ev, fn, false);
                } else if (el.attachEvent) {
                    el.attachEvent('on' + ev, fn);
                } else {
                    el['on' + ev] = fn;
                }
            }
            addEvent(window, 'load', function() {
                log('inside the second window.onload handler in "private section"');
            });
        })(window); 
    </script>​
    

    DEMO

    The example of code organization you asked about:

    HTML:

    <ol id="res"></ol>​

    JavaScript:

    /* app.js */
    
    // in global scope:    
    var MyApp = (function(app) {
        var res = document.getElementById('res');
    
        app.log = function(line) {
            var li = document.createElement('li');
            li.innerHTML = line;
            res.appendChild(li);
        };
    
        app.doWork = function() {
            app.log('doing a work');
        };
    
        return app;
    })(MyApp || {});
    
    /* my-app-module.js */
    
    // again in global scope: 
    var MyApp = (function(app) {
    
        app.myModule = app.myModule || {};
    
        app.myModule.doWork = function () {
            app.log('my module is doing a work');
        };
    
        return app;
    })(MyApp || {});
    
    /* somewhere after previous definitions: */
    
    (function() {
        MyApp.doWork();
        MyApp.myModule.doWork();
    })();
    

    ​DEMO

    MyApp is accessible from outside
    Nothing is accessible from outside

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

Sidebar

Related Questions

I'm currently learning about structs, so I have the following exercise: Set a Struct
I found the following semaphore example. I'm currently learning about thread syncing. I have
I am currently learning about basic networking in java. I have been playing around
I am currently learning C# and LINQ. I have lots of questions about them.
I am currently learning about widgets in Android. I want to create a WIFI
I'm new to JavaScript, and I'm currently learning about the so-called for... in loop.
I currently learning about scrum and want to learn from experienced professionals in the
I'm currently learning more about CSRF and I have a basic question about cookies.
I'm currently learning after a book about how to convert xaml code into objects
I'm currently learning about pointers in my C++ book (Programming: Principles and Practice using

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.