modzsi modzsi - 6 months ago 188
Javascript Question

HOWTO: Simulate click OK in WebBrowser alert/messagebox that initiated by a JavaScript? (Delphi)

I have an app with a WebBrowser inside:

when I POST my webpage i have javascript popup alert/messagebox comes on where I need to click OK. Here is my javascript that creates the alert:

function delete(){
if (confirm('Are you sure you wish to delete this ?')){
document.forms.item.action = "edit.asp?action=delete";
document.forms.item.submit();
}

}


I was searching for a while but could not yet find any working solution...

Thanks in advance for all your help!

Answer

Implement IDocHostShowUI::ShowMessage and show your own dialog, or just return S_OK.

Note:The link is broken. Here the code for the solution:

      IDocHostShowUI = interface(IUnknown)
        ['{c4d244b0-d43e-11cf-893b-00aa00bdce1a}']
        function ShowMessage(hwnd: THandle; lpstrText: POleStr; lpstrCaption: POleStr;
          dwType: longint; lpstrHelpFile: POleStr; dwHelpContext: longint;
          var plResult: LRESULT): HRESULT; stdcall;
      end;

      TShowMessageEvent = function(Sender: TObject; HWND: THandle;
        lpstrText: POleStr; lpstrCaption: POleStr; dwType: Longint; lpstrHelpFile: POleStr;
        dwHelpContext: Longint; var plResult: LRESULT): HRESULT of object;

      TWebBrowser = class(SHDocVw.TWebBrowser, IDocHostShowUI)
        private
          fOnShowMessage: TShowMessageEvent;
        protected
          function ShowMessage(HWND: THandle; lpstrText: POleStr; lpstrCaption: POleStr;
            dwType: Longint; lpstrHelpFile: POleStr; dwHelpContext: Longint;
            var plResult: LRESULT): HRESULT; stdcall;
        published
          property OnShowMessage: TShowMessageEvent read fOnShowMessage write
            fOnShowMessage;
      end;

function TWebBrowser.ShowMessage(HWND: THandle; lpstrText, lpstrCaption: POleStr;
  dwType: Integer; lpstrHelpFile: POleStr; dwHelpContext: Integer;
  var plResult: LRESULT): HRESULT;
begin
  if Assigned(fOnShowMessage) then
    Result := fOnShowMessage(Self, HWND, lpstrText, lpStrCaption, dwType,
      lpStrHelpFile, dwHelpContext, plResult)
  else
  Result:= S_OK;
end;
Comments