2

Here is HTML code :

<script type='text/javascript'>//<![CDATA[ 
    window.onload=function(){
    alert('hi');
    }//]]>  

</script>

Not only alerts, popups, new tab, new window but anything cause focus change.
By other words I don't want WebBrowser control to get focused or change current focus.

Looking for a solution that doesn't hurt any weppage( I need javascript enabled).

Mohsen Sarkar
  • 5,910
  • 7
  • 47
  • 86
  • You will have to intercept the page load event for the browser control, parse the javascript for anything that generates a popup, alert, etc. and comment that code out. You will also have to do that for any javascript that is loaded from a URL. It has been a while since I used the webbrowser control, so I am not certain this is possible with the built-in control. – Adam Zuckerman Feb 20 '14 at 04:48
  • And there will be places where doing the capture will interfere with the web page. – Adam Zuckerman Feb 20 '14 at 04:49
  • @AdamZuckerman Do you know if I can fix this issue with CEF or any other browser control ? – Mohsen Sarkar Feb 20 '14 at 04:50
  • I don't know what CEF is. None of the other browser controls that I am aware of will do this. – Adam Zuckerman Feb 20 '14 at 04:51
  • There is a good page with browser control alternatives. [Replacing .NET WebBrowser control with a better browser, like Chrome?](http://stackoverflow.com/questions/790542/replacing-net-webbrowser-control-with-a-better-browser-like-chrome) – Adam Zuckerman Feb 20 '14 at 04:56

1 Answers1

1

In theory, you should implement IProtectFocus, but apparently there're issues with it:

http://social.msdn.microsoft.com/Forums/windows/en-US/211733a0-af84-4cc4-9851-2f19def7aedd/iprotectfocus-and-allowfocuschange-not-working-on-webbrowser?forum=winforms

Another option might be to implement a CBTHook, handle HCBT_SETFOCUS and reject it (return 1 to prevent it) if the window becoming focused is a child window of the WebBrowser object.

Disclaimer: I myself haven't tried either of the above.

noseratio
  • 59,932
  • 34
  • 208
  • 486