简体   繁体   中英

JavaScript eval

Why doesn't this eval call alert("Summer") ?

eval('(caption="Summer";alert(caption))');

Does it have something to do with the quotes in "Summer"?

Uncaught SyntaxError: Unexpected token ;

The outer parentheses make no syntactical sense. Try this:

eval('caption="Summer";alert(caption)');

Chrome console:

eval('(caption="Summer";alert(caption))')
SyntaxError: Unexpected token ;

This works:

eval('caption="Summer"; alert(caption)')

额外的括号错了,这是正确的:

eval('caption="Summer";alert(caption)');

更好的方法是执行此操作并避免语法问题,请执行以下操作,

eval(function(){var x="test"; alert(x)}());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM