How can I get this eval() call to work in IE?

拈花ヽ惹草 提交于 2019-12-23 03:21:43

问题


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

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

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

   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.

Thanks, Pete


回答1:


Have you tried:

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

...?




回答2:


This worked with good browsers and bad ones (which means 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.

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.

// myfeed.php

return json_encode($myarray);

// myjs.js

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

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



来源:https://stackoverflow.com/questions/1050840/how-can-i-get-this-eval-call-to-work-in-ie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!