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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:43:29+00:00 2026-06-06T22:43:29+00:00

After discovering about Javascript namespaces, I tried to implement them but I run into

  • 0

After discovering about Javascript namespaces, I tried to implement them but I run into a problem while trying to attach a namespace method to an element’s onclick.

I used this method to wrap up my functions/methods/classes (a simplified concept, not my actual code):

;(function(window, undefined) {

    //my namespace
    var NS = {};

    NS.test = {
        f : function(param) {
            alert(param);
        }
    }

    NS.test.('test 2');

})(window);

Inside, everything works fine and “test 2” is prompted.

However, when I try to attach that function to a click event, by doing something like this:

<a href-"#" onclick="NS.test.f('test');">Click me!</a>

it doesn’t work, just like it doesn’t work when I call that function after the })(window); part.

I tried it calling it window.NS.test.f('test'); but with no effect.

How can I make an onclick event call my function?

I could attach an event listener inside my wrapper, like I do for other html elements with no difficulty, but it would be problematic in this case since I’m generating the links with javascript and I find it easier and simpler to just add onclick="doSomething" for all my links, instead of creating them, then cache them and add event listeners.

Call me lazy, but in this particular case I prefer to do

someDiv.innerHTML = my_Generated_Html_Code_With_OnClick;

instead of

//demo code, ignore the flaws and the fact it won't work on IE
someDiv.innerHTML = my_generated_Html_code;
myLink = document.getElementById(id);
myLink.addEventListener('mousedown', NS.test.f('test'));

I do not use any framework nor do I wish to, since I’m trying to get a better understanding of the so-called vanilla javascript first.

I set up a jsfiddle here.

P.S. I must admit I didn’t understand namespaces completely so if I’m doing something wrong here or applying the concept in a way I am not supposed to, I would appreciate any tips or corrections

  • 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-06T22:43:30+00:00Added an answer on June 6, 2026 at 10:43 pm

    That’s because NS is declared inside and hence only exists inside the function:

    function(window, undefined) {
        var NS = {};
    
        // NS exists here ...
    }
    
    // ... but not here
    

    If you want to make it available to the rest of the page, then you can do:

    function(window, undefined) {
    
        var NS = window.NS = {};
    
        // NS and window.NS exist here ...
    }
    
    // ... and window.NS exists here.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am just learning how to use Terracotta after discovering it about a month
After discovering on superuser that there is no current way to add syntax highlighting
After recently discovering how insanely long the .net 3.5 framework was to install I
After some time researching and trying different things I still cannot get my @ExceptionHandler
After a long search I'm still confused about it although I found some related
After discovering the Boost preprocessor's capabilities I found myself wondering: Is the C99 preprocessor
After discovering lambda expressions, and their use as anonymous functions, I've found myself writing
I solved this myself, but I spent so long discovering such a simple solution,
After doing an AJAX call, am redirecting- using the following code. But, after AJAX
I'm trying to get my ad hoc build distributed but have started experiencing problems.

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.