3

Is there any alternative of ExecuteGlobal in javascript?

Function vbExecuteGlobal(parmSCRIPT)
    ExecuteGlobal(parmSCRIPT)
End Function

DevGuru [describes the statement] such:

The ExecuteGlobal statement takes a single string argument, interprets it as a VBScript statement or sequence of statements, and executes these statements in the global namespace.

5
  • 2
    If something executes a string as code, the solution is to refactor it so it doesn't. You should not keep code in strings.
    – Quentin
    Jul 13, 2012 at 9:35
  • You "could" use setTimeout(code, 0)... but are you sure you really need this? I agree with Quentin here. Jul 13, 2012 at 9:35
  • @FelixKling — I was going to suggest that (with dire warnings), but there is a minimum timeout in most JS engines, and it evaluates the code asynchronously.
    – Quentin
    Jul 13, 2012 at 9:38
  • Can you please clarify, do you want to evaluate javascript in javascript globally or vbscript in javascript?
    – Esailija
    Jul 13, 2012 at 9:39
  • actually i am doing cross browser codeing and i have some old code which is in mixture of vbscript & javascript and except IE none of the browser support vbscript.So they are calling this vbscript function in javascript which working fine only in IE so how can i do in javascript... i want to evaluate javascript in javascript or any other solution you have.. thanks
    – Abhishek
    Jul 13, 2012 at 10:31

1 Answer 1

1

The Javascript equivalent to VBScript's Execute[Global] is eval(). The passed code is evaluated in the context of the call.

See here for details, pros and cons

UPDATE

Not to recommend such practices, but to clarify my understanding of equivalence:

// calling eval in global context is the exact equivalent of ExecuteGlobal
eval("function f0() {print('f0(): yes, we can!');}");
f0();

// calling eval in locally is the exact equivalent of Execute
function eval00() {
  eval("function f1() {print('f1(): no, we can not!');}");
  f1();
}
eval00();
try {
  f1();
}
catch(e) {
  print("** error:", e.message);
}

// dirty trick to affect global from local context
function eval01() {
  eval("f2 = function () {print('f2(): yes, we can use dirty tricks!');}");
  f2();
}
eval01();
f2();

output:

js> load("EvalDemo.js")
f0(): yes, we can!
f1(): no, we can not!
** error: "f1" is not defined.
f2(): yes, we can use dirty tricks!
f2(): yes, we can use dirty tricks!

So: Problems that can be solved using Execute[Global] in VBScript can be solved using eval() in Javascript; for some problems extra work or tricks may be necessary.

As Abhishek explicitly said "i want to evaluate javascript in javascript", I don't feel the need to justify my answer.

1
  • 1
    Even in your answer you say that it evaluates in the context of the call (not global) so you must know it's not equivalent. So why the answer?
    – Esailija
    Jul 13, 2012 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.