[WinForm] WebBrowser blocking Script Error prompt, winformwebbrowser

Source: Internet
Author: User

[WinForm] WebBrowser blocking Script Error prompt, winformwebbrowser

In actual development, you will find that setting the ScriptErrorsSuppressed attribute cannot prevent script errors, but you can implement it in the following two ways.

1. subscribe to Error handling in the DocumentCompleted event as follows:

        private void wbGoogleMap_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)        {            wbGoogleMap.Document.Window.Error += new HtmlElementErrorEventHandler(Window_Error);        }        void Window_Error(object sender, HtmlElementErrorEventArgs e)        {            e.Handled = true;        }
2. Handle the window. onerror in the script as follows:
        window.onerror = function(error, url, line) {//            log(error + "url:" + url + "lineNo:" + line);            return true;        }
In this way, the WebBrowser Script Error prompt can be effectively blocked, and I hope it will be helpful!
C # How does winform shield webBrowser pop-up windows and script errors? Do not hesitate to share your skills

// Shield some pop-up windows
Void wb_NavigateComplete2 (object pDisp, ref object URL)
{
Mshtml. IHTMLDocument2 doc = (wb. ActiveXInstance as SHDocVw. WebBrowser). Document as mshtml. IHTMLDocument2;
Doc.parentWindow.exe cScript ("window. alert = null", "javascript ");
Doc.parentWindow.exe cScript ("window. confirm = null", "javascript ");
Doc.parentWindow.exe cScript ("window. open = null", "javascript ");
Doc.parentWindow.exe cScript ("window. showModalDialog = null", "javascript ");
Doc.parentWindow.exe cScript ("window. close = null", "javascript");} For details, see www.cnblogs.com/...2.html

When a webpage is loaded with webBrowser1, a js error occurs in the webpage, and a prompt is displayed. How to block it?

WebBrowser control disables hyperlink redirection, Script Error prompt, default right-click menu, and shortcut key

Since VS2005, The WebBrowser control provided by VS has been quite friendly and highly controllable. Winform works with WebBrowser for UI development. It is also a very smooth mode. Microsoft vs ide installation programs are basically in this mode.

Error script disabling prompt
Set ScriptErrorsSuppressed of the WebBrowser control to true.

Disable right-click menu
Set IsWebBrowserContextMenuEnabled of WebBrowser to false

Disable shortcuts
Set WebBrowserShortcutsEnabled of WebBrowser to false

Disable hyperlink
There are two types of hyperlinks: Direct redirection of the current window and opening of the new window.
Of course, direct window redirection:
Set AllowNavigation of WebBrowser to false
Open in a new window:

To disable the opening of a new window, you must handle the NewWindow event of WebBrowser.
Private void webBrowser1_NewWindow (object sender, CancelEventArgs e)
{
E. Cancel = true;
}

After the above work is done, it is basically finished. The last point should be noted, that is, Drag-And-Drop.

Remember to set AllowWebBrowserDrop of WebBrowser to false.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.