Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why write `window[ "eval" ].call( window, data );`

Tags:

jquery

Line 614 of jQuery 1.7rc1:

window[ "eval" ].call( window, data );

Why not simply write

eval.call( window, data );?

like image 346
Randomblue Avatar asked Feb 03 '23 12:02

Randomblue


2 Answers

The answer is here: Decoding jQuery,

Jim Driscoll found out that for more standards-respecting browsers, you could use eval.call(window,data), but for Chrome and IE, things are a bit different.

Internet Explorer: It seems that IE uses window.execScript(data)

Chrome: eval.call(window,data) breaks on Chrome, but window[ "eval" ].call( window, data) works on Chrome, and as well as other non-IE browsers, this is how the above workarounds based upon.

like image 89
scrappedcola Avatar answered Feb 05 '23 18:02

scrappedcola


After looking at the source, I have found this link. Have a look at the emphasized text:

Sadly, eval.call(window,src) breaks on Chrome - it complains about contexts not matching. Odd - and I was unable to Google up why this might be so. But a couple lucky guesses later, and I discovered that window.eval.call(window,src) works on all non-IE browsers. Now, when I say "var j = 1", the window[j] is the variable that's set... So, that's good. Why do we have to add the extra window. on Chrome? Not sure - I could guess, but it's too likely to be wrong.

So, window.eval is used to get globalEval work in Chrome.

like image 44
Rob W Avatar answered Feb 05 '23 18:02

Rob W