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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:09:54+00:00 2026-06-12T19:09:54+00:00

I currently have a Web Application that runs off a global Javascript-based API, and

  • 0

I currently have a Web Application that runs off a global Javascript-based API, and it is initialized like this:

var Api = {
    someVar: "test",
    someFunction: function() {
        return "foo";
    }
}

This API is shared across many “Widgets” that live in the Web Application, and they should all run off this single Api instance so they can pass data to each other.

AJAX is currently used to load these Widgets, for example in widgets/mywidget.html, and it’s placed in, say, <div id='widget_<random number>'>...</div>

Certain other parts of the code may choose to add more functionality to Api, and it’s currently done like this:

Api.myExtension = {
    myNewFunction: function() {
        return "bar";
    }
}

However, some issues arise from this kind of usage:

Problem One: What if one Widget (these may be provided by third-parties) decides to hide some code within, and does something similar to Api = {}, destroying the global Api var everything lives on, and breaking the whole Application? Is it possible to protect this Api variable from being overwritten from outside? Only “extending” is allowed (adding new things), but “removing/changing” is not allowed. i.e.:

Api.foo = { test: "bar" } // allowed
Api.someVar = "changing the existing someVar"; // not allowed

The following code is located “inside” Api, for example:

var Api = {
    Debug: {
        Messages = new Array,
        Write: function() {
            Api.Debug.Messages.push("test"); // allowed
        }
    }
}

Api.Debug.Messages.push("test 2"); // not allowed

Probable Solutions I’ve Thought Of:

  1. Suppose we simply use frames to resolve this issue. The Apis provided are now separate from each other. However, there’s additional overhead when loading Api again and again if I have many Widgets running, and they can no longer communicate with the “Host” of the widgets (the page where frames reside in), for example, I may want to tell the host to show a notification: Api.Notify.Show("Test"), but it cannot do so because this Api is completely independent from other instances, and it cannot communicate with the “Host”

  2. Using something like a “getter” and “setter” function for the Api to be read and written. I’m unsure on how to implement this, so any help on directions on how to implement this is welcome!

  3. A mixture of 1/2?

  • 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-12T19:09:56+00:00Added an answer on June 12, 2026 at 7:09 pm

    The only way (at least that I can think of) to protect your global variable is to prevent the Widgets from having a direct access to it. This can be achieved by using frames functions, as you suggested. You should create an object that contains all the functions that the Widgets should be able to use, and pass such to each Widget. For example:

    var Api = {
       widgetApi = {
          someFunction: function(){
              // ...
          }
       },
       addWidget:function(){
          var temp = this.widgetApi.constructor(); 
    
          for(var key in this.widgetApi)
             temp[key] = clone(this.widgetApi[key]);
          return temp;
       }
       // Include other variables that Widgets can't use
    }
    

    This way, the Widgets could execute functions and communicate with the host or global variable Api. To set variables, the Widget would be editing its private object, rather than the global one. For every frame (that represents a Widget), you must initialize or create a copy of the widgetApi object, and probably store it inside an array, in such a way that an instance of a Widget is stored in the main Api object.

    For example, given <iframe id="widget"></iframe>

    You would do the following:

    var widget = document.getElementById("widget");
    widget.contentWindow.Api = Api.addWidget();
    widget.contentWindow.parent = null;
    widget.contentWindow.top = null;
    

    Additionally, in every frame you would need to set the parent and top variables to null so that the Widgets wouldn’t be able to access the data of the main frame. I haven’t tested this method in a while, so there might be ways to get around setting those variables to null.

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

Sidebar

Related Questions

We have a web application that uses dropbox as file storage. We have currently
I have a web application that can load plugins through reflection. It currently uses
I'm currently writing a web application that have about 6-12 pages. On each one
I am currently working on MVC4 SinglePage Application. I have a Web Api method
I have a web application that runs perfectly fine when I use the Visual
I have a Grails 2.0.0 web application that runs in both a DEV and
Here's what I have: A web application that runs in a single HTML page
I have an application that runs on a server under Solaris. This application exports
I am trying to move a web application that currently runs on WebSphere 6.1
I have an web application that displays employee birthdays for the current month in

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.