如何让这个eval()调用在IE中工作?

时间:2021-12-24 08:16:56

I have some javascript that goes out and fetches a javascript "class" on another xhtml page. The remote javascript looks something like the following:

我有一些javascript出来并在另一个xhtml页面上获取一个javascript“类”。远程javascript看起来如下所示:

    (function() {
        this.init = function() {
            jQuery("#__BALLOONS__tabs").tabs();
        };
    })

After this is fetched into this.javascript, I try to eval it and instantiate:

在将此文件提取到this.javascript之后,我尝试评估它并实例化:

   this.javascript = eval("(" + this.javascript + ")");
   this.javascript = new this.javascript();
   this.javascript.init();

Of course, this works perfectly in all browsers except IE. In IE, it fails at the eval line. Does anyone have suggestions on how I can make this work in IE or an alternative.

当然,这在IE以外的所有浏览器中都能很好地工作。在IE中,它在评估线上失败。有没有人有关于如何在IE或其他方面使这项工作的建议。

Thanks, Pete

5 个解决方案

#1


Have you tried:

你有没有尝试过:

eval("this.javascript = (" + this.javascript + ")");

...?

#2


This worked with good browsers and bad ones (which means ie) :

这适用于良好的浏览器和坏的浏览器(这意味着ie):

var code_evaled;
function eval_global(codetoeval) {
    if (window.execScript)
        window.execScript('code_evaled = ' + '(' + codetoeval + ')',''); // execScript doesn’t return anything
    else
        code_evaled = eval(codetoeval);
    return code_evaled;
}

Enjoy

#3


(eval is not an object method in IE). So what to do? The answer turns out to be that you can use a proprietary IE method window.execScript to eval code.

(eval不是IE中的对象方法)。那么该怎么办?答案结果证明您可以使用专有的IE方法window.execScript来评估代码。

function loadMyFuncModule(var stufftoeval) {
  var dj_global = this; // global scope reference
  if (window.execScript) {

    window.execScript("(" + stufftoeval + ")");

    return null; // execScript doesn’t return anything
  }
  return dj_global.eval ? dj_global.eval(stufftoeval) : eval(stufftoeval);
}

#4


If worst truly comes to worst, something like this may work:

如果最坏的情况真的变得最糟糕,这样的事情可能有效:

var self = this;
funcid = "callback" + Math.random();
window[funcid] = function(javascript) {
  delete window[funcid];
  self.javascript = javascript;
  self.javascript = new self.javascript();
  self.javascript.init();
}
document.write("<script language='javascript'>" +
               "window." + funcid + "(" +
                 "(" + this.javascript + "));" +
               "</script>");

#5


I had the same problem of eval() with IE, and the function with "window.execScript" didn't worked for me. The solution I found to get arrays in javascript from a page (php in my case), was to use some JSON.

我有与IE的eval()相同的问题,而“window.execScript”的功能对我没有用。我发现从一个页面(在我的情况下是php)中获取javascript数组的解决方案是使用一些JSON。

// myfeed.php

return json_encode($myarray);

// myjs.js

$.getJSON('myfeed.php',function(data){dataAlreadyEvaled = data;});

This needs no eval() function, if it helps anyone...

这不需要eval()函数,如果它可以帮助任何人......

#1


Have you tried:

你有没有尝试过:

eval("this.javascript = (" + this.javascript + ")");

...?

#2


This worked with good browsers and bad ones (which means ie) :

这适用于良好的浏览器和坏的浏览器(这意味着ie):

var code_evaled;
function eval_global(codetoeval) {
    if (window.execScript)
        window.execScript('code_evaled = ' + '(' + codetoeval + ')',''); // execScript doesn’t return anything
    else
        code_evaled = eval(codetoeval);
    return code_evaled;
}

Enjoy

#3


(eval is not an object method in IE). So what to do? The answer turns out to be that you can use a proprietary IE method window.execScript to eval code.

(eval不是IE中的对象方法)。那么该怎么办?答案结果证明您可以使用专有的IE方法window.execScript来评估代码。

function loadMyFuncModule(var stufftoeval) {
  var dj_global = this; // global scope reference
  if (window.execScript) {

    window.execScript("(" + stufftoeval + ")");

    return null; // execScript doesn’t return anything
  }
  return dj_global.eval ? dj_global.eval(stufftoeval) : eval(stufftoeval);
}

#4


If worst truly comes to worst, something like this may work:

如果最坏的情况真的变得最糟糕,这样的事情可能有效:

var self = this;
funcid = "callback" + Math.random();
window[funcid] = function(javascript) {
  delete window[funcid];
  self.javascript = javascript;
  self.javascript = new self.javascript();
  self.javascript.init();
}
document.write("<script language='javascript'>" +
               "window." + funcid + "(" +
                 "(" + this.javascript + "));" +
               "</script>");

#5


I had the same problem of eval() with IE, and the function with "window.execScript" didn't worked for me. The solution I found to get arrays in javascript from a page (php in my case), was to use some JSON.

我有与IE的eval()相同的问题,而“window.execScript”的功能对我没有用。我发现从一个页面(在我的情况下是php)中获取javascript数组的解决方案是使用一些JSON。

// myfeed.php

return json_encode($myarray);

// myjs.js

$.getJSON('myfeed.php',function(data){dataAlreadyEvaled = data;});

This needs no eval() function, if it helps anyone...

这不需要eval()函数,如果它可以帮助任何人......