Cross Frame Script Reuse
I have an application running in a HTA (MSFT HTML Application) that uses the same script file over and over again throughout frames; as th开发者_如何学Gois hits 9 in places and as the application is setup within various servers with caching set to immediate expire I'm trying to carve out some sort of performance in this ball of mud.
Is there a 'good' way to load the main script file in the top frame then excuting it within the frames i.e.
--- TOP WINDOW ----
var MainScript = function(){ return (function(){ all current functions etc here })(); };
--- SUB Frames ----
var FrameScript = top.MainScript;
FrameScript();
And how would this be affected by window scope (would it keep the top window scope or be in scope of the frame-window)
The simplest method appears to be to give the subframe an ID then to dynamically populate it with the script loaded in the master frame (using eval to make the js run);
i.e.
|> Parent (aka TOP Frame)
<script>top.windows = [];</script>
<script id="MyScript">
var test = function(){ top.windows.push(window); }
</script>
|>> SubFrame Loads SubSubFrame in an IFrame
|>>> SubSubFrame
<script id="SF1">
document.getElementById("SF1").innerHTML = eval(top.window.document.getElementById("MyScript").innerHTML);
test();
</script>
This Works upto and beyond 8 frames deep within a trusted domain (in a .hta you set application=true on the frames for this)
I used top.windows[] so I could check the scope. (type it into console.log(top.windows) in firebug)
Nausiating and deep; Google do something similar to delay JS loading/execution.
- http://googlecode.blogspot.com/2009/09/gmail-for-mobile-html5-series-reducing.html
- http://blog.sproutcore.com/post/225219087/faster-loading-through-eval
精彩评论