Use mshtml to obtain the function return value in Javascript

Source: Internet
Author: User
Today I figured out how to use mshtml to obtain the return value of the function in Javascript. We used the execScript method without the mshtml. IHTMLWindow2 interface to execute javascript code segments in HTML documents, as shown in

 

// AwbMain is the AxSHDocVw. AxWebBrowser Control
Mshtml. IHTMLDocument2 doc = this. awbMain. Document;
Private mshtml. IHTMLWindow2 win = doc. parentWindow;
String strScript = "alert ('OK ');";
Win.exe cScript (strScript, "javascript ");

 

The intelligent prompt shows that the type of Return Value of the execScript method is object, but its return value is always null (of course, we should not trust the smart prompt of COM. Many parameters and return values are all object types, can not get more detailed information), even if the method executes a javascript function with a returned value.

How can this problem be solved? Google it. There is no answer to this question on the simplified Chinese page. Please refer to the English document (here, many people miss the opportunity to learn something because they are lazy or don't use google or other search engines. Use search engines more, consider how to use keywords, and constantly improve the quality of keywords. I believe it is easy to find the appropriate information ). Enter "javascript return value" + mshtml + c # "as the keyword. The search result contains only one document. This document describes in detail how.. NET application to control Internet Explorer. For example, there are both C # and VB. NET, both 1.0 and 2.0, very good (attached to the end of the essay ).

The document provides a solution to this problem, but it is a solution in. NET 2.0 .. NET 2.0 is implemented using the InvokeScript method of the HtmlDocument class:

 

String strRetVal = "";
// AwbMain is the System. Windows. Forms. WebBrowser control.
HtmlDocument doc = this. awbMain. Document;
// JsFunction is the javascript function name in the HTML document.
StrRetVal = (string) doc. InvokeScript ("jsFunction ");
System. Windows. Forms. MessageBox. Show (strRetVal );

 

Note that the javascript function name in the HTML document must be followed by an empty parenthesis (without parameters), and that empty parentheses are not required for calling in C #2.0.

How to solve this problem by using mshtml in C #1. x? Here we will introduce a work ing method (these work ins have also been used in previous articles ):
1. Define variables in Form to store the return values of javascript functions;
2. Use InteropServices in. NET 1.x to pass the Form object of the application to the html dom;
3. assign a value to the variable in 1 in the javascript function (assign the same value as the return value of the javascript function), so that the javascript function does not even need to return values.

The code for Step 2 is as follows:

 

Private System. Runtime. InteropServices. Expando. IExpando iex;
Private mshtml. IHTMLDocument2 doc = null;
// AwbMain is the AxSHDocVw. AxWebBrowser control. The Code executed after the wendang is loaded
Private void awbMain_DocumentComplete (object sender, AxSHDocVw. DWebBrowserEvents2_DocumentCompleteEvent e)
{
This.doc = (mshtml. IHTMLDocument2) this. awbMain. Document;
Ex = (System. Runtime. InteropServices. Expando. IExpando) doc;
System. Reflection. PropertyInfo piform = ex. AddProperty ("MainForm ");
Piform. SetValue (doc, this, null );
}

 

In this way, the document. MainForm can be used in the javascript function of the HTML document in step 3 to refer to the Form in the Windows application.

 

From: http://www.cnblogs.com/waxdoll/archive/2007/06/30/271018.html

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.