JavaScript functions are called through the Cwebpage class in VC/MFC (to pass parameters to the JS function and get the return value)

Source: Internet
Author: User
Tags script tag

Transferred from: http://www.cnblogs.com/javaexam2/archive/2012/07/14/2632959.html

① needs a class cwebpage written by someone else , adding its two files WebPage.h and WebPage.cpp to the project.

Cwebpage class header file and implementation file:

http://download.csdn.net/detail/masikkk/4427190

② Add the webBrowser control, #include "webbrowser2.h" in the header file of the View/dialog class , increase the member variable m_webbrowser, and call the Navigate () function to load a HTML page.

③ in the dialog box or view implementation file, add #include "WebPage.h", in the response function of the button can call JavaScript function, the specific code is as follows:

cwebpage web;

web. Setdocument (M_webbrowser.getdocument ());

CComVariant Varresult; Store return value

Note: Parameters are passed in string type

web. Calljscript ("Tansto", M_latitude, M_longitude, &varresult);  //Call js function tansto (), and pass 2 parameters (latitude, longitude varresult

if (varresult dblval = = 122.211) //double type corresponding dblval, shaping corresponding intval

MessageBox (" success in obtaining variable values from JS ");

④ defines the corresponding function in the script tag of the HTML file, with the following code:

<script type= "Text/javascript" >

function Tansto (lat, LNG)//go to the specified position with two parameters, latitude and longitude

{

Map.panto (New glatlng (lat, LNG)); Google Maps API, jump to coordinates specified location

return 122.211; Returns the value of a double type to varresult

}

</script>

Attention:

(1) the panto () function in JavaScript above is a function in the Google Map API that can customize the relevant function according to different needs.

(2) The above implementation does not need to tube cwebpage class what role, directly with the line, if you want to learn more about the content, please search for "COM component Automation" "IDispatch interface" article.

JavaScript functions are called through the Cwebpage class in VC/MFC (to pass parameters to the JS function and get the return value)

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.