C ++ WebKit calls Javascript

Source: Internet
Author: User

To call JavaScript Functions in C ++ code, you must first obtain reference to the JavaScript Functions to be called.

Step 1: Obtain the JavaScript context that defines the function. I have not tried winlaunche. In gtklauncher, I use the following method:

Frame = webkit_web_view_get_main_frame (web_view );
Context = webkit_web_frame_get_global_context (FRAME );

Winlauncher should be similar.

Step 2: Go to the Global Object of the context. Assume that the JavaScript function you defined is global.

Jsobjectref globalobject = jscontextgetglobalobject (context );

After obtaining the global object, a JS character reference object jsstringref containing the global function name to be called is required. Assume that the JavaScript function my_function is declared as follows:

Function my_function (A, B)
{

// Do something

Alert (A + B );

}

Step 3: Get reference of JavaScript function "my_function"

First create a string my_function:

Jsstringref myfunctionname = jsstringcreatewithuf8cstring ("my_function ");
Then, use the function name "my_function" to obtain the reference functionobject of the function from the Global Object attribute.
Jsobjectref functionobject = (jsobjectref) jsobjectgetproperty (context, globalobject, myfunctionname, null );

Step 4: Call JavaScript Functions

In the following example, assume that the global JavaScript function my_function contains two numeric parameters:

Jsvalueref arguments [2];
Jsvalueref result;
Int num_arguments = 2;
Arguments [0] = jsvaluemakenumber (context, 1.0 );
Arguments [1] = jsvaluemakenumber (context, 3.14 );

Result = jsobjectcallasfunction (context, functionobject, globalobject, num_arguments, arguments, null );

Compile gtklauncher with the following command: GCC 'pkg-config -- cflags -- libs webkit-1.0 GTK +-1000' main. C-S-O gtklauncher

Start gtklancher and call the JavaScript function my_function. In the alert prompt box, 4.14 is displayed.

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.