Note that there are some explanatory texts on larger screens.

plurals
  1. POcan I load a javascript file under a context other than 'window'?
    text
    copied!<p>I try to load some external .js files, and have some irresolvable namespace conflicts.</p> <p>I had the idea of loading some of the files in their own context somehow, replacing the "<code>this</code>" from pointing at the window object to some custom namespace.</p> <p>example:</p> <p>first.js:</p> <pre><code>name = "first"; </code></pre> <p>second.js:</p> <pre><code>name = "second"; </code></pre> <p>It seems to me that this kind of trick can be very useful. Is it possible at all?</p> <p><strong>EDIT</strong><br> seems that replacing "<code>this</code>" does not begin to solve the problem, as it is not the default context for identifier resolution in javascript. this is my test code:</p> <pre><code>var first = {}; var second = {}; (function(){name = "first";}).call(first); (function(){name = "second";}).call(second); document.write('name= '+name+' &lt;br/&gt;\n'); //prints "second" document.write('first.name= '+first.name+' &lt;br/&gt;\n'); //prints "undefined" document.write('second.name= '+second.name+' &lt;br/&gt;\n'); //prints "undefined </code></pre> <p>any ideas?</p> <p><strong>RESOLUTION</strong><br> It is not possible. I ended up wiser than I was this morning, and I gave it up. I recommend these enlightening reading materials for anyone with a similar problem that might want to take a crack at it: <a href="http://jibbering.com/faq/notes/closures/" rel="nofollow">http://jibbering.com/faq/notes/closures/</a><br> <a href="http://softwareas.com/cross-domain-communication-with-iframes" rel="nofollow">http://softwareas.com/cross-domain-communication-with-iframes</a></p>
 

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