javascript - How to change global context for new Function(...)? -
i specify global context given function whilst leaving local scope intact. after research turned out close , working:
// iframe created via document.write share objects var iframeglobal = myiframeglobal; (function( ns ) { var context = this; // iframe's global, correct! }( iframeglobal )); now said, retain scope of function in iframe context intact:
var realglobal = window;// creator source of iframe var iframeglobal = myiframeglobal; (function( ns ) { // iframe's global, correct! var context = this; // button custom html element in iframe, owner document // correct. custom widget, js has been parsed // in iframe well. var mywidget = button; //the content of new function var script = "var context = window; console.dir(context); return context"; var fn = new function("{" + script + "}"); var result = fn.apply(mywidget, _args || {});//returns realglobal }( iframeglobal )); so might see, changing global context did work, context did fall parent document when new function has been parsed.
is on purpose? saw answers here mentions behavior doesn't makes sense me since 'this' correct , functions below should traverse it.
ps:
- this tested against firefox-29 , firebug 1.12(must run there)
- actually, want run 'scripts' containing $('#otherbutton').css(...) in iframe, within custom scope passed via fn.apply(mywidget,...);
to bind function context have use bind(object).
var realglobal = window;// creator source of iframe var iframeglobal = myiframeglobal; (function( ns ) { // iframe's global, correct! var context = this; // button custom html element in iframe, owner document // correct. custom widget, js has been parsed // in iframe well. var mywidget = button; //the content of new function var script = "var context = window; console.dir(context); return context"; var fn = new function("{" + script + "}").bind(this); var result = fn.apply(mywidget, _args || {});//returns realglobal }( iframeglobal ));
Comments
Post a Comment