Lex,

Have you tried using the string constructor for Function()?  I believe  
that it creates a function whose lexical scope is bound to the top- 
level, no matter where it is executed.

<script>
var a = 2;

function x() {
   var a = 3;
   return new Function("alert(a); a = 4;");
}

x()();
alert(a);

function y() {
   var a = 10;
   x()();
}
y();
</script>

This alerts "2", "4", "4"  in Firefox, Safari, Opera and IE6.

On 5-Mar-09, at 1:19 PM, Lex Spoon wrote:

>
> For possible amusement, I spent half a day yesterday spelunking into
> the world of various ways to install code in a web browser.  Details
> are here:
>
> http://lexspoon.blogspot.com/2009/03/many-scopes-of-javascripts-eval.html
>
>
> My initial idea to use window.eval turns out to be terribly
> non-portable: it only evaluates at window scope on Firefox.  Instead,
> I'm thinking to use the following code to install newly downloaded
> code:
>
> if (window.execScript) {
>  window.execScript(script)
> } else {
>  var tag = document.createElement("script")
>  tag.type = "text/javascript"
>  tag.text = script
>  document.getElementsByTagName("head").item(0).appendChild(tag)
> }
>
>
> The first branch is for window.execScript, which is available on IE
> and, of all things, Chrome.  They are such similar browsers, you know.
> The second branch works on all browsers, but it's ugly enough that I
> didn't want it to be used all the time.  Maybe that's silly, and only
> the second branch should always be used.
>
> Any thoughts or better ideas?
>
> Lex
>
> >


--~--~---------~--~----~------------~-------~--~----~
http://groups.google.com/group/Google-Web-Toolkit-Contributors
-~----------~----~----~----~------~----~------~--~---

Reply via email to