JavaScript Calls from C++
×î½üÒªÓõ½Ïà¹Ø¼¼Êõ£¬ÏÈÌùÔÚÕ⣬ÓпÕÔÙ·Ò³¡£
±¾ÎÄת×Ô£º
http://www.codeguru.com/Cpp/I-N/ieprogram/article.php/c4399
http://www.codeguru.com/cpp/i-n/ieprogram/article.php/c4399/JavaScript-Calls-from-C.htm
http://www.codeproject.com/KB/COM/jscalls.aspx
Introduction
Sometimes, when we are using the IE Browser Control inside of a C++ application, we need to access the HTML elements. We can do it by using standard COM objects such as IWebBrowser2 , IHTMLDocument2 , and so forth. By doing this, we easily can implement features such as click button, click anchor, get input string, get HTML text, and so on. Unfortunately, Microsoft did not provide similar objects for JavaScript. In any case, it is possible to make a control for the JavaScript object inside an HTML page by using a traditional COM approach. This article describes the class CWebPage that allows you to do it and a technique to call a JavaScript function from C++ code.
How to Do This
As the result of using the presented class, it will be easy to call any JavaScript function from C++ code. To implement this feature, we should get a pointer to the IHTMLDocument2 interface. If we are using the CHtmlView class from MFC, we can get one by using member function CHtmlView::GetHtmlDocument() . In the case of using the IWebBrowser or IWebBrowser2 components, the function get_Document will bring us the desired interface. Here is an example:
CComPtr<IDispatch> spDisp = CHtmlView::GetHtmlDocument();
m_webPage.SetDocument(spDisp);
The rest of the things will be done by the CWebPage class. Here is an example of a JavaScript call without parameters.
m_webPage.CallJScript("Welcome");
The example of the JavaScript call with two parameters will look like this:
m_webPage.CallJScript("Miltiply","2.34","3.32");
The Class Implementation
class CWebPage
{
public:
CWebPage();
virtual ~CWebPage();
bool SetDocument(IDispatch* pDisp);
LPDISPATCH GetHtmlDocument() const;
const CString GetLastErr
Ïà¹ØÎĵµ£º
1£®ÔÚCOM×é¼þÖе÷ÓÃJavaScriptº¯Êý
// Á¬½Óµã·½Ê½Ò³Ãæjavascript½Å±¾
<object classid="CLSID:B568F111-DFE4-4944-B67F-0728AB2AB30F"
id="testCom" VIEWASTEXT></object>
<script language="JavaScript" for="testCom" event="staTe(s)">
&nbs ......
µ±ÐèÒªÔÚÍøÒ³ÖÐÏÔʾʱ¼äʱ¿ÉÒÔÓõ½ÏÂÃæÕâ¶Î´úÂ룺
<SCRIPT language=JavaScript type=text/JavaScript>
var day="";
var month="";
var ampm="";
var ampmhour="";
var myweekday="";
var year="";
mydate=new Date();
myweekday=mydate.getDay();
mymonth=mydate.getMonth()+1;
myday= mydate.getDate(); ......
1¡¢×Ö·û´®×ª»»ÎªÊýÖµ
³£¹æ·½·¨£º
JScript code
var a = parseFloat("12");
var b = parseInt("34");
¿ÉÒÔÓÃ
JScript code
var a = +("12");
var b = +("34");
2¡¢¶àάÊý×é
³£¹æ·½·¨£º
JScript code
var A = new Array(2);
A[0] = new Array(2);
A[1] = new Array(2);
A[0][0] = 1;
A[0][1] = 2;
A[1][0] = ......
³£¼ûµÄÄÚ´æ·ÖÅäºÍʹÓôíÎó
1) ÄÚ´æµÄÉêÇëºÍ·ÖÅ䲢ûÓгɹ¦£¬µ«³ÌÐòԱȴʹÓÃÁËËü¡£Ò»Ð©ÐÂÊÖ¾³£»á·¸ÕâÖÖ´íÎó£¬ËûÃDz¢²»»áÁôÒâµ½ÄÚ´æûÓзÖÅä³É¹¦¡£ÅжÏÖ¸ÕëµÄÖµÊÇ·ñΪNULL¿ÉÒÔÓÐЧµØ±ÜÃâÕâÖÖ´íÎó¡£
2) ÄÚ´æµÄ·ÖÅäÒѾ³É¹¦£¬µ«ÊÇȴûÓнøÐгõʼ»¯¾ÍÖ±½ÓʹÓÃËüÁË¡£Ê×ÏÈÊ ......
º¯ÊýÊdzÌÐòµÄÖ´ÐÐÄ£¿é£¬ÊÇÕû¸öÏîÄ¿µÄϸ°û£¬ÈçºÎ×éÖ¯ºÃÿһ¸öº¯ÊýµÄ½á¹¹£¬ÈçºÎÌá¸ß³ÌÐòµÄ¿É¶ÁÐÔºÍÔËÐÐЧÂÊ£¬ÊdzÌÐòÔ±ÃDz»¶ÏƷζµÄÖ÷Ìå¡£ÓÐʱº¯ÊýµÄÉè¼ÆÄÜÖ±½ÓÌåÏÖ³ö³ÌÐòÔ±¶ÔC/C++µÄÀí½âºÍ¸ÐÎò¡£
C/C++µÄº¯ÊýÀàÐͲ»¶ÔÍâºõÒÔϼ¸ÖÖ£¬ÓÐЩÃèÊö¿ÉÄܲ»Ì«ÌùÇУº
......