Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>AS Josh Stodola mentioned creating the variable at runtime isn't the problem</p> <pre><code>var holdsWidgetUUID = "widgetUUIDValue"; eval(holdsWidgetUUID + "= (" + data + ")"); alert(eval(holdsWidgetUUID)); </code></pre> <p>Or if you prefer</p> <pre><code>var UUID = "widgetUUID"; var holdsWidgetUUID = "widgetUUIDValue"; window["widgets"] = new Array(); window["widgets"][holdsWidgetUUID] = data; alert(window["widgets"][holdsWidgetUUID]); </code></pre> <p>The problem is getting the loaded javascript to work an be callable like dynamicvariablename.methodname()</p> <p>I have a working solution if you force a certain coding practice upon the included javascript. Maybe this gets you in the right direction.</p> <p>This is a widgets javascript (works.js). Notive that it's a javascript "class" with internally defined fields and methods. Which by itself keeps namespace pollution low and allows us the achieve the desired calling form x.getInfo()</p> <pre><code>function () { this.type = 1; this.color = "red"; this.getInfo = function() { return this.color + ' ' + this.type + ' widget'; }; } </code></pre> <p>And this is the file which includes it at runtime in a namespace</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;Widget Magic?&lt;/title&gt; &lt;script type='text/javascript' src='jquery.js'&gt;&lt;/script&gt; &lt;script type='text/javascript'&gt; var UUID = "widgetUUID"; var holdsWidgetUUID = "widgetUUIDValue"; window["widgets"] = new Array(); $.get("works.js", function(data) { window["widgets"][holdsWidgetUUID] = eval("(" + data + ")"); $(document).ready(function() { window["widgets"][holdsWidgetUUID] = new (window["widgets"][holdsWidgetUUID])(); alert(window["widgets"][holdsWidgetUUID].color); alert(window["widgets"][holdsWidgetUUID].getInfo()); }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;p&gt;Just some stuff&lt;/p&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    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