Note that there are some explanatory texts on larger screens.

plurals
  1. POClearing custom variables in the window object in Javascript
    primarykey
    data
    text
    <p>This is a tricky problem which has had me scratching my head for a few days.</p> <p>I'm working on a project which involves taking an ten year old web application and reworking it as a single-page application. The application is huge - the time we have to work, quite tight, so some shortcuts had to be made.</p> <p>Overall, however, I'm quite impressed with how far we've come, as we've had to overcome some interesting technical hurdles.</p> <p>One involved clearing all custom window variables. Since we're dynamically reloading different pages of the application, we need to clear all custom variables so we don't get conflicts. What we do is firstly, load the base bootstrap of the application and save all the properties on the window object in an array.</p> <p>Then, before we load each new page, we loop through the window properties and clear all the objects that aren't in our saved array (taking the window state back to before the page was loaded).</p> <p>Now, this works fine in all the browsers we've tested except for IE7 and IE8 (which both need to be supported). The issue seems to be that global variables don't always seem to register on the window object.</p> <p>Does anyone have any insight onto this problem? Any idea how to tackle for this IE7?</p> <p>Any info will be appreciated.</p> <p>EDIT: On bootstrap load we do this:</p> <pre><code>for (i in window) { this.globalVars[i] = 1; } </code></pre> <p>And then when we load a new page (via AJAX), we do:</p> <pre><code>for (i in window) { if (!this.globalVars[i]){ window[i] = undefined; } } </code></pre> <p>FINAL SOLUTION:</p> <p>In the end the easiest fix given the limited time was to simply change all variables defined as var x; to var x = null;</p> <p>There was another solution I found, however. There was a small library here which I used as a starting point for an alternate solution: <a href="http://www.thomasfrank.se/global_namespace.html" rel="nofollow">http://www.thomasfrank.se/global_namespace.html</a></p> <p>It's not perfect (may need some tweaking to make it a little more stable, like adding a try-catch block around the AJAX call for example so that cross-domain scripts don't crash it). The way it works is that all external scripts files and internal scripts are parsed through, extracting a ton of words which can then be used to clear properties from the window object.</p> <p>We actually experienced something very strange - this script wasn't properly picking up a lot of our variables... It turns out that it uses document.scripts to get all the loaded scripts on the page to be able to loop through them and parse them. The issue is that jQuery doesn't load external pages on the page in that way. What it does is simply pass the code to exec from what I know. As such, no script tag is actually added to the page.</p> <p>The fix for this is to parse the raw AJAX response and store a reference to all the script tags (as well as, I suppose, extract the inline scripts) and then modify the library to be able to handle these files. This should work but all this processing going on was too scary for speed reasons - finding that we could simply do a search &amp; replace on all variable definitions and have most of the job without the requirement of copious amounts of work for each page load made it obvious which path we should take.</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.
 

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