0

出于可访问性的原因,对于我的 Chrome 扩展,我需要能够用window.onbeforeunload页面内 HTML 替代品替换所有标准弹出窗口。我知道如何创建一个替代骨架,这很简单。

我知道这里的基本思想是window.onbeforeunload = {my substitute code}

我不知道该怎么做是从页面发出的每个确认窗口中获取文本和代码,并将其引导到我的替代品中,以便显示与原始弹出窗口中相同的消息并单击我的替代品confirmleave page(或其他)按钮产生与原始弹出窗口相同的结果。

如何才能做到这一点?


例子:

你安装了我的扩展(不要问它的目的是什么,无关紧要)。

您开始在 StackOverflow 上写一个问题,然后意识到您已经知道答案并关闭选项卡。

通常,会出现一个确认窗口,询问您是否确定要离开该页面。

但是由于我的扩展,整个页面的 HTML 被向下推了一下,以便为临时 HTML 确认框腾出空间,显示相同的文本和相同的两个按钮,单击时会产生相同的两个结果,全部在页内. 没有弹出窗口。

4

1 回答 1

2

The feature you're talking about is the window.onbeforeunload event, and unfortunately the only way it can be customized is to provide a custom string message for the user, because the potential for abuse is so high.

The api reference over at msdn for Internet Explorer states:

The default statement that appears in the dialog box, "Are you sure you want to navigate away from this page? ... Press OK to continue, or Cancel to stay on the current page.", cannot be removed or altered.

I take this to mean the dialog itself cannot be altered, all you can do is provide an additional context-specific message. I cant locate the same reference for Chrome, but it seems to be the same story there.

window.onbeforeunload = function() {
    //all you can do is provide a message..
    return "you have unsaved changes, if you leave they will be lost";
}
于 2013-09-12T21:05:35.393 回答