0

I'm interested in knowing the limits of JavaScript's eval method.

eval("console.log('This is a test')");

will work as expected, printing the text This is a test in the browser's dev-console. But what about this code:

eval("jQuery(window).on('load', function(){ console.log('This is a test'); });");

Would that cause the browser to print the text after the page is loaded?

What about using jQuery's globalEval? Would that make any difference?

Regards

EDIT: I'm doing some tests using this: https://stackoverflow.com/a/745386/940158 Even after evaling the jQUery code, the handler doesn't show up when I do $.eventReport(window), which makes me believe that eval is failing at some point.

Community
  • 1
  • 1
alexandernst
  • 14,352
  • 22
  • 97
  • 197
  • 3
    _"Would that cause the browser to print the text after the page is loaded?"_ What's stopping you from trying? – Cerbrus Jul 02 '14 at 09:55
  • @Cerbrus I tried and it doesn't, now I'm looking for an explication if it's possible and if not, why. – alexandernst Jul 02 '14 at 09:55
  • @alexandernst: Probably because you evaled that code after the `load` event did fire. Try using `jQuery(document).ready(` instead. – Bergi Jul 02 '14 at 12:02

1 Answers1

0

evil() Literally (attempts to) run the code provided in it's parameter.
Any JavaScript that works, should work when wrapped in a string, in a evil().

As long as jQuery exists at the moment you're calling the second line, the code should execute just fine. Even though the load event might have passed already, at that time.
(So, the event listener will be added, but might not fire)

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • I don't know enough about `globalEval` to answer that part of the question, I'm afraid. – Cerbrus Jul 02 '14 at 10:00
  • Ok, I'm asking all this because I'm making some tests with http://stackoverflow.com/a/745386/940158 and even after eval-ing the jQuery code, the handler doesn't show up when I do `$.eventReport(window)`, which makes me think that `eval` is failing somewhere. – alexandernst Jul 02 '14 at 10:04
  • Why are you even `evil`-ing anything? What do you expect that to do? – Cerbrus Jul 02 '14 at 10:06
  • I'm just curious about the limits of `eval` and I'm searching a reasonable answer about either why it works or why it doesn't work. – alexandernst Jul 02 '14 at 10:07
  • `evil` literally starts a new JavaScript interpreter, and has it interpret the passed string. It not working might not even be related to `evil`. – Cerbrus Jul 02 '14 at 10:08
  • So you mean the context of `eval` is different when running code than the context that is already loaded? That doesn't make too much sense because I can call an already loaded function from `eval`, like this: `eval("my_function();")`. – alexandernst Jul 02 '14 at 10:10
  • I just mean it literally starts a new interpreter (--> Horribly inefficient). Code in the `evil` still has access to the scope "outside" of it. – Cerbrus Jul 02 '14 at 10:12
  • Ok, so it should work, but it doesn't. Now I'd like to know *why* it doesn't work. "It starts a new interpreter" is not a valid answer as I already demonstrated that the new interpreter shares the same context. – alexandernst Jul 02 '14 at 10:13
  • Replace the `.on('load'` with `.on('click'`, see if that works. Better yet, add a normal `onload` with a `console.log`, and check if the `evil` is executed before or after the `onload` event fires. My guess is that it doesn't work, simply because the event's passed already. – Cerbrus Jul 02 '14 at 10:15
  • I understand your concern about the event being already fired, really. The question is that even after manually checking *what* events are binded to the `window` object (with the link I posted a few replies earlier), there are *no* handlers at all, which means that the `eval` failed. – alexandernst Jul 02 '14 at 10:17
  • So, are you certain `jQuery` exists, when call the `evil`? – Cerbrus Jul 02 '14 at 10:19