1

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!

1
  • Use a tool like Winspector to identify the class or title of the window that the alert appears in. Then create a timer on your form to watch for that window appearing, and then use SendMessage to send it a mousedown or similar.
    – mj2008
    Aug 9, 2012 at 15:14

3 Answers 3

1

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;
1
  • That is actually the best solution for my problem: `function CommandTarget_Exec(CmdGroup: PGUID; nCmdID, nCmdexecopt: DWORD; const vaIn: OleVariant; var vaOut: OleVariant): HRESULT; begin if nCmdID = OLECMDID_SHOWMESSAGE then begin Result:=S_OK; vaOut:=true; exit; end; end;'
    – modzsi
    Mar 14, 2016 at 6:40
1

If this is for very restricted, internal, usage, you can do a dirty

procedure TForm1.Timer1Timer(Sender: TObject);
const
  TargetCaption = 'Meddelande från webbsida';
var
  S: string;
  len: integer;
begin
  SetLength(S, 127);
  len := GetWindowText(Application.ActiveFormHandle, PChar(S), 127);
  if len = 0 then Exit;
  SetLength(S, len);
  if S = TargetCaption then
    SendMessage(Application.ActiveFormHandle, WM_COMMAND, ID_OK, 0);
end;

where TargetCaption is the known caption of the TWebBrowser popup, confirm, or prompt dialogs. This may vary between OS versions and language versions, so this approach is only acceptable in a very restricted, in-house application, where it is OK to 'update' the application with every new Windows SP...

By the way, "Meddelande från webbsida" is Swedish for "Message from web page".

4
  • Tack så mycket! Great answer, I am just testing your solution right now.
    – modzsi
    Aug 9, 2012 at 21:03
  • Worked fine! Good luck for Svenska in the handball final! :) We just lost to you yesterday.
    – modzsi
    Aug 11, 2012 at 9:31
  • Is there a way to catch the text from the website and press NO on a Yes/No dialog from IE? Tack!
    – modzsi
    Apr 23, 2013 at 16:25
  • ID_NO and ID_YES got it :)
    – modzsi
    May 9, 2013 at 9:00
-2

Buttons on alert() confirm() and prompt() boxes are not scriptable. Use a HTML/CSS modal dialog instead.

2
  • Thanks for your answer. Can you be more specific please?
    – modzsi
    Aug 9, 2012 at 20:58
  • -1 Actually, you can override alert() and confirm() boxes with IDocHostShowUI::ShowMessage Dec 28, 2013 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.