自动javascript确认框 [英] Automating javascript confirmation box

查看:57
本文介绍了自动javascript确认框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,当弹出一个javascript确认警报/框时,浏览器将专注于该框,而禁用所有其他功能,直到选择了两个选项之一为止;单击确定",则返回true;如果选择取消",则返回true.单击它会返回false.

As is known, when a javascript confirmation alert/box pops up, the browser focuses on the box leaving everything else disabled until one of two options are chosen, when "OK" is clicked it returns true, when "Cancel" is clicked it returns false.

现在,当编写涉及确认警报的脚本时,我的陈述是基于这些布尔值的返回,现在我想知道(我不知道这是否是一个愚蠢的问题),这里仅在javascript中强制或自动返回true或false?

Now when writing scripts that involve confirmation alerts, my statements are based on the return of these booleans, now I am wondering (I do not know if this is a dumb question), ist here a way in javascript alone to force or automate the return of either true or false?

例如,我编写了一个脚本来检测确认警报,然后在脚本中强制其返回true或false.

For instance, I write a script to detect a confirmation alert, and then force it to return true or false in my script.

注意:我只是javascript的初学者,所以我不知道这是否是一个好问题,但是我希望得到一个答案.

Note: I am a mere beginner in javascript, so I do not know if this is a good question or not, but I would appreciate an answer.

推荐答案

不在页面内.

JavaScript是单线程的,并且 confirm alert 处于阻塞状态.他们等待输入时不会运行任何JS.

JavaScript is single threaded and confirm and alert are blocking. No JS will run while they are waiting for input.

例如,如果您为此编写测试,则可以尝试模拟整个 confirm alert 函数.

If you were, for instance, writing tests for this, then you could try mocking the entire confirm or alert function.

window.confirmValue = true;
window.confirm = function () { return window.confirmValue; }

if (confirm("Hello, world")) {
  console.log("True");
}

如果您是从页面外部编写JS(例如驱动PhantomJS),则方法可能会有所不同.

If you were writing JS from outside the page (e.g. to drive PhantomJS) then the approach might be different.

这篇关于自动javascript确认框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆