Permalink
Browse files

strip out iframeCapable code that doesn't work in favor of the execSc…

…ript() trick for IE
  • Loading branch information...
1 parent 7c0b2c4 commit ee622726af50bbd08403fb45b093cf0b86d03a60 @substack committed Feb 27, 2012
Showing with 11 additions and 46 deletions.
  1. +9 −43 index.js
  2. +1 −1 testling/run.js
  3. +1 −2 testling/test.sh
View
@@ -19,62 +19,28 @@ var Script = exports.Script = function NodeScript (code) {
this.code = code;
};
-var iframe = document.createElement('iframe');
-if (!iframe.style) iframe.style = {};
-iframe.style.display = 'none';
-
-var iframeCapable = true; // until proven otherwise
-if (navigator.appName === 'Microsoft Internet Explorer') {
- var m = navigator.appVersion.match(/\bMSIE (\d+\.\d+);/);
- if (m && parseFloat(m[1]) <= 9.0) {
- iframeCapable = false;
- }
-}
-
Script.prototype.runInNewContext = function (context) {
if (!context) context = {};
- if (!iframeCapable) {
- var keys = Object_keys(context);
- var args = [];
- for (var i = 0; i < keys.length; i++) {
- args.push(context[keys[i]]);
- }
-
- var fn = new Function(keys, 'return ' + this.code);
- return fn.apply(null, args);
- }
+ var iframe = document.createElement('iframe');
+ if (!iframe.style) iframe.style = {};
+ iframe.style.display = 'none';
document.body.appendChild(iframe);
- var win = iframe.contentWindow
- || (window.frames && window.frames[window.frames.length - 1])
- || window[window.length - 1]
- ;
+ var win = iframe.contentWindow;
forEach(Object_keys(context), function (key) {
win[key] = context[key];
- iframe[key] = context[key];
});
- if (win.eval) {
- // chrome and ff can just .eval()
- var res = win.eval(this.code);
- }
- else {
- // this works in IE9 but not anything newer
- iframe.setAttribute('src',
- 'javascript:__browserifyVmResult=(' + this.code + ')'
- );
- if ('__browserifyVmResult' in win) {
- var res = win.__browserifyVmResult;
- }
- else {
- iframeCapable = false;
- res = this.runInThisContext(context);
- }
+ if (!win.eval && win.execScript) {
+ // win.eval() magically appears when this is called in IE:
+ win.execScript('null');
}
+ var res = win.eval(this.code);
+
forEach(Object_keys(win), function (key) {
context[key] = win[key];
});
View
@@ -2,7 +2,7 @@ var test = require('testling');
var vm = require('./');
test('vmRunInNewContext', function (t) {
- //t.plan(4);
+ t.plan(5);
t.equal(vm.runInNewContext('a + 5', { a : 100 }), 105);
View
@@ -7,6 +7,5 @@ else
fi
curl -sSNT <(tar -cf- ../index.js run.js 2>/dev/null) \
- 'http://testling.com/?main=run.js&browsers=iexplore/8.0' \
+ 'http://testling.com/?main=run.js&browsers=chrome/17.0,iexplore/9.0,firefox/10.0,safari/5.1,opera/11.6' \
-u "$user"
-# 'http://testling.com/?main=run.js&browsers=chrome/17.0,iexplore/8.0,iexplore/9.0,firefox/10.0,safari/5.1,opera/11.6' \

0 comments on commit ee62272

Please sign in to comment.