C # shielding the pop-up window and Script Error prompt in webbrowser

Source: Internet
Author: User
I. Blocking script error messages   When the IE browser encounters a script error, a yellow icon will appear in the lower-left corner of the browser. You can click to view the detailed information about the script error. No error message box is displayed. When we use the webbrowser control, an error message box is displayed. Program Obviously unfriendly, And will suspend some automatically executed programs. I can see that the solution taken is to create a form killer program to close the pop-up form. The method discussed today is to solve the problem from the control.

1. shdocvw. dll

The webbrowser control we used in the com era is shdocvw. dll. The method for blocking error information is easy to use.

 
Webbrowser1.silent =True;

2.. net

In. net, the managed webbrowser is provided for our use. Of course, we can still use com in. Net to build shdocvw. dll. If shdocvw. dll is used
The error handling method is the same as the preceding method. But how can we solve this problem by using the. NET component?

This component provides us with a method scripterrorssuppressed. However, it does not work in. Net framework2.0. It is said that the following solution is used in earlier versions.

 
Webbrowser1.scripterrorssuppressed =True;

(It is said that this was the case before. Net framework2.0 and I have never used it)

So how can we solve this problem in. Net framework2.0?

One method cannot be completely solved. We can solve some of the problems here.
// Capture control errors
This. webbrowser. Document. Window. Error + = new htmlelementerroreventhandler (window_error );
// Handle errors
Void window_error (Object sender, htmlelementerroreventargs E)
{
// Self-processingCode
E. Handled = true;
}

3. The above method cannot be well solved in the case of nesting multiple frameworks.

To completely solve this problem, we use axwebbrowser to solve the webbrowser problem.

We define a class of our own. Its parent class is webbrowser. We can use this class later. Shdocvw must be referenced in the definition of this class.
Class ewebbrowser: system. Windows. Forms. webbrowser
{
Shdocvw. iwebbrowser2 iwb2;

Protected override void attachinterfaces (Object nativeactivexobject)
{
Iwb2 = (shdocvw. iwebbrowser2) nativeactivexobject;
Iwb2.silent = true;
Base. attachinterfaces (nativeactivexobject );
}

Protected override void detachinterfaces ()
{
Iwb2 = NULL;
Base. detachinterfaces ();
}
}

 

 

// Add the Micrsoft. mshtml reference to the project
Using mshtml;

Private void webbrowserinclunavigated (Object sender, webbrowsernavigatedeventargs E)
{
Ihtmldocument2 vdocument = (ihtmldocument2) webbrowser1.document. domdocument;
Vdocument.parentwindow.exe cscript (
"Function alert (STR) {If (STR = 'zswang ') Confirm (STR );}","Javascript");
}

// Frame Structure

Private void webbrowserinclunavigated (Object sender, webbrowsernavigatedeventargs E)
{
Ihtmldocument2 vdocument = (ihtmldocument2) webbrowser1.document. domdocument;
Foreach (ihtmlelement velement in vdocument. All)
If (velement. tagname. toupper () = "frame ")
{
Ihtmlframebase2 vframebase2 = velement as ihtmlframebase2;
Vframebase2.contentwindow.exe cscript (
"Function alert (STR) {confirm ('[' + STR + ']');}", "JavaScript ");
}
}

2. Shielding other windows

(WB. activexinstance as shdocvw. webbrowser). navigatecomplete2 + = new dwebbrowserevents2_navigatecomplete2eventhandler (wb_navigatecomplete2); // WB is a webbrowser Control

// 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 ");

Iii. Automatic confirmation dialog box

Q: How to enable auto-click the OK button in the webbrowser pop-up dialog box in winform?

A:

// using mshtml;
// using shdocvw;
private void form1_load (Object sender, eventargs e)
{< br> This. webbrowser1.navigate ("http: // localhost: 28512/website2/default. aspx ");
shdocvw. webbrowser WB = This. webbrowser1.activexinstance as shdocvw. webbrowser;
WB. navigatecomplete2 + = new shdocvw. dwebbrowserevents2_navigatecomplete2eventhandler (wb_navigatecomplete2);

}

Void wb_navigatecomplete2 (Object Pdisp, ref object URL)
{
Mshtml. ihtmldocument2 Doc = (this. webbrowser1.activexinstance as shdocvw. webbrowser). Document as mshtml. ihtmldocument2;
Doc.parentwindow.exe cscript ("function alert (STR) {return''} "," JavaScript ");
}

 

 

 

Transferred from:Http://www.cnblogs.com/oldsea/archive/2009/07/31/1536252.html

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.