Note that there are some explanatory texts on larger screens.

plurals
  1. POAvoiding pollution of globals via iframe script loader?
    primarykey
    data
    text
    <h2>Problem...</h2> <p>Poorly-coded scripts exist which need to be included on a web page.</p> <p>These scripts pollute the global scope by doing things like:</p> <ul> <li>Assigning values to undeclared identifiers </li> <li>Adding properties to built-in constructor functions (like <code>Object</code> and <code>Array</code>) and their prototypes</li> <li>Other nasty stuff. </li> </ul> <hr> <h2>Solution?</h2> <p>I want to include the scripts without the adverse side effects. I think it can be achieved by loading the script in an iframe and exporting objects as properties of the parent window. Here's what Ive got so far:</p> <pre><code>&lt;script&gt; (function(){ var g=this, frameIndex=frames.length, f=document.createElement('iframe'); // hide it like this instead of display:none, because some old browser ignores // iframes with display:none, or is this an ancient habit I can drop? f.style.width='0px'; f.style.height='0px'; f.style.border='none'; f.style.position='absolute'; // append it to document.body or document.documentElement? // documentElement seems to work before body is loaded, // but is it cross-browser safe? document.body.appendChild(f); // window object for our iframe var w=frames[frameIndex]; // callback function pulls the object into the current window when script loads w.cb=function(){ g.SomeObject=w.SomeObject }; // will this work on IE, or do I need to use document.createElement? // wanted to avoid document.createElement in this case because I'm not sure // whether to call it from window.document or frames[frameIndex].document w.document.innerHTML='&lt;script onload="cb()" src="myscript.js"&gt;&lt;\/script&gt;'; }()); &lt;/script&gt; </code></pre> <hr> <h2>Questions:</h2> <ul> <li><p>Will there be potential havoc if a script modifies built-in prototypes and I move it into another window, or will my parent window's built-ins stay clean and everything will "just work?"</p></li> <li><p>Is this idea going to work on 'most' browsers, or is there a show-stopper? Haven't tested on anything besides chrome and moz so far.</p></li> <li><p>I'd like to remove the iframe after pulling the object into the current window, but moz will lose the object reference if the iframe is removed. Does anyone know of a way around that?</p></li> <li><p>Has this already been done, or is there a better way to accomplish my goal? If so, what's the name of the script or technique I should to be looking for?</p></li> </ul> <p>(question transplanted from <a href="https://stackoverflow.com/questions/3828423/haxe-for-javascript-without-global-namespace-pollution">here</a>)</p>
    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.
 

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