Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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:</p> <pre><code>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 } </code></pre> <p>This way, the Widgets could execute functions and communicate with the <em>host</em> or global variable <code>Api</code>. 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 <em>initialize</em> or create a copy of the <code>widgetApi</code> object, and probably store it inside an array, in such a way that an <em>instance</em> of a Widget is stored in the main <code>Api</code> object.</p> <p>For example, given <code>&lt;iframe id="widget"&gt;&lt;/iframe&gt;</code></p> <p>You would do the following:</p> <pre><code>var widget = document.getElementById("widget"); widget.contentWindow.Api = Api.addWidget(); widget.contentWindow.parent = null; widget.contentWindow.top = null; </code></pre> <p>Additionally, in every frame you would need to set the <code>parent</code> and <code>top</code> 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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload