Można użyć metody IOleCommandTarget
, aw jej metodzie IOleCommandTarget.Exec
można przechwycić polecenie OLECMDID_SHOWSCRIPTERROR
.
W poniższym przykładzie użyłem klasy pośredniej, więc jeśli umieścisz ten kod w swoim urządzeniu, tylko te przeglądarki internetowe w formularzu lub te utworzone w tej jednostce dynamicznie uzyskają to zachowanie.
uses
SHDocVw, ActiveX;
type
TWebBrowser = class(SHDocVw.TWebBrowser, IOleCommandTarget)
private
function QueryStatus(CmdGroup: PGUID; cCmds: Cardinal; prgCmds: POleCmd;
CmdText: POleCmdText): HRESULT; stdcall;
function Exec(CmdGroup: PGUID; nCmdID, nCmdexecopt: DWORD;
const vaIn: OleVariant; var vaOut: OleVariant): HRESULT; stdcall;
end;
implementation
function TWebBrowser.QueryStatus(CmdGroup: PGUID; cCmds: Cardinal;
prgCmds: POleCmd; CmdText: POleCmdText): HRESULT; stdcall;
begin
Result := S_OK;
end;
function TWebBrowser.Exec(CmdGroup: PGUID; nCmdID, nCmdexecopt: DWORD;
const vaIn: OleVariant; var vaOut: OleVariant): HRESULT; stdcall;
begin
// presume that all commands can be executed; for list of available commands
// see SHDocVw.pas unit, using this event you can suppress or create custom
// events for more than just script error dialogs, there are commands like
// undo, redo, refresh, open, save, print etc. etc.
// be careful, because not all command results are meaningful, like the one
// with script error message boxes, I would expect that if you return S_OK,
// the error dialog will be displayed, but it's vice-versa
Result := S_OK;
// there's a script error in the currently executed script, so
if nCmdID = OLECMDID_SHOWSCRIPTERROR then
begin
// if you return S_FALSE, the script error dialog is shown
Result := S_FALSE;
// if you return S_OK, the script error dialog is suppressed
Result := S_OK;
end;
end;
Ta metoda również blokuje wszystkie wyskakujące okienka javascript. – TipTop
Czy masz przykładową stronę, która zachowuje się w ten sposób? Zobacz artykuł ['this'] (http://support.microsoft.com/kb/261003). Czy na pewno nie ma błędu, zanim pojawi się wyskakujące okienko? IMHO powinno tłumić tylko błędy, ale mogę spojrzeć ... – TLama
@ TipTop, ogólnie rzecz biorąc, kod nie ma nic wspólnego z wyskakującymi wyskakującymi skryptami JavaScript. Jeśli masz problem z kodem, myślę, że domyślną wartością zwracaną nie powinno być S_OK, ale OLECMDERR_E_NOTSUPPORTED. – stanleyxu2005