且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否存在不会暂停脚本的JavaScript警报?

更新时间:2023-10-29 23:22:16

你可以在setTimeout(非常短暂的超时)中执行警报,因为setTimeout是异步的:

You could do the alert in a setTimeout (which a very short timeout) as setTimeout is asynchronous:

setTimeout("alert('hello world');", 1);

或者为了正确地做到这一点,你真的要在你的setTimeout中使用方法而不是字符串:

Or to do it properly you really show use a method rather than a string into your setTimeout:

setTimeout(function() { alert('hello world'); }, 1);

否则你会打开JavaScript注入攻击。传递字符串时,它将通过JavaScript eval 函数运行。

Otherwise you open yourself up to JavaScript injection attacks. When you pass a string it is run through the JavaScript eval function.