views:

1514

answers:

4

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

A: 

(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);
}
Janie
Thanks, but I've already tried this. According to Microsoft's docs, this method always returns null. http://msdn.microsoft.com/en-us/library/ms536420(VS.85).aspx
slypete
Yes, but you can access it in the global scope regardless (see my edit)
Janie
When I say access "it" i'm referring to the object you've created by evalling.
Janie
I'm afraid that this is still not a complete answer. The "class" that I'm loading (not object) has no name. How would I instantiate this class that I loaded if I have no handle?
slypete
Try it out.....
Janie
Try what out? I need to create an instance. var object = new (???)(); As you can see, handle is missing.
slypete
Don't be a dick Pete, just try it, trust me ;)
Janie
Hey Pete, you can use Jquery eval too, that'll fix the x-browser issues
Janie
I'm not being sarcastic. How would you do it? I have no idea. There is no handle.
slypete
A: 

Have you tried:

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

...?

Lee Kowalkowski
Well there is no eval in IE. I did try this with window.execScript, however, but it doesn't work either unfortunately.
slypete
There is no eval in IE!?!? Of course there is! eval is discouraged, but is part of the JS/EcmaScript specification, and has been in IE since IE3 (JScript version 1): http://msdn.microsoft.com/en-us/library/2z6exc9e.aspx
Lee Kowalkowski
You must be doing something else wrong to have the perception that eval isn't available.
Lee Kowalkowski
Thanks Lee, so if that's the case I don't understand why eval returns null only in IE.
slypete
Hi Lee, this ended up working after I made the variable local. Don't ask me why it didn't like the instance var. Anyway, thanks!
slypete
@slypete, thank you very much for that 'working after i made the variable local' remark, it literally saved my butt. I hate IE!!
Rosdi
A: 

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>");
fuzzyTew
A: 

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

sebastien