最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript eval - Stack Overflow

programmeradmin7浏览0评论

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

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

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

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

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

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

Share Improve this question edited Sep 22, 2011 at 13:40 Richard JP Le Guen 28.8k8 gold badges93 silver badges120 bronze badges asked Feb 14, 2010 at 23:33 JamesBrownIsDeadJamesBrownIsDead 1152 silver badges4 bronze badges 2
  • Why do you have parentheses around all the stuff inside the quotes? – Anon. Commented Feb 14, 2010 at 23:35
  • If you want to sequence statements inside an expression use the , operator: eval('(caption="Summer",alert(caption))'); – Dominic Cooney Commented Feb 15, 2010 at 0:50
Add a comment  | 

4 Answers 4

Reset to default 13
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)')

The extra parentheses are wrong, this is correct:

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

A more better way to do this and avoid syntax problem is to do following,

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论