Bạn có thể sử dụng IOleCommandTarget
và trong phương thức IOleCommandTarget.Exec
của chúng tôi bắt lệnh OLECMDID_SHOWSCRIPTERROR
.
Trong ví dụ sau tôi đã sử dụng lớp xen kẽ nên nếu bạn đặt mã này vào đơn vị của mình, chỉ những trình duyệt web trên biểu mẫu hoặc các trình duyệt được tạo trong đơn vị này sẽ có hành vi này.
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;
Phương pháp này cũng ngăn chặn tất cả các cửa sổ bật lên javascript. – TipTop
Bạn có trang mẫu nào hoạt động theo cách này không? Xem bài viết ['this'] (http://support.microsoft.com/kb/261003). Bạn có chắc chắn không có lỗi trước khi cửa sổ bật lên được hiển thị không? IMHO nó sẽ chỉ chặn các lỗi, nhưng tôi có thể xem xét ... – TLama
@TipTop, nói chung, mã không có gì để làm với các cửa sổ bật lên được gọi JavaScript. Nếu bạn gặp sự cố với mã, tôi nghĩ giá trị trả lại mặc định không được là S_OK, nhưng OLECMDERR_E_NOTSUPPORTED. – stanleyxu2005