Best way to pass data to javascript

Do not post support requests, bug reports or feature requests. Discuss CEF here. Non-CEF related discussion goes in General Discussion!

Best way to pass data to javascript

Postby DJackson » Thu Jun 23, 2016 6:36 pm

I'm trying initiate a download upon a button press and then send the downloaded html to a javascript function to be displayed in my window. However, I can't seem to get the html to the javascript.

The native function is being called from the button press (name=="login").
The request is being initialized.
The request callback is being called.
The data (html) is being downloaded.
The javascript function (changeContent) is being called.
No method I have tried thus far to get the html to that function to be displayed has worked.
The alert box which should be displaying some content is displaying "undefined".

In my V8Handler:
Code: Select all
         else if (name == "login")
            CefRefPtr<CefRequest> request = CefRequest::Create();

            // Set the request URL.

            // Set the request method. Supported methods include GET, POST, HEAD, DELETE and PUT.

            const std::string& upload_data = "";
            CefRefPtr<CefPostData> postData = CefPostData::Create();
            CefRefPtr<CefPostDataElement> element = CefPostDataElement::Create();
            element->SetToBytes(upload_data.size(), upload_data.c_str());

            // Create the client instance.
            CefRefPtr<MyRequestClient> client = new MyRequestClient(0, app_, object);

            // Start the request. MyRequestClient callbacks will be executed asynchronously.
            CefRefPtr<CefURLRequest> url_request = CefURLRequest::Create(request, client.get(), NULL);

In my request client:
Code: Select all
   class MyRequestClient : public CefURLRequestClient {
      MyRequestClient(int request_type, CefRefPtr<Application> app = NULL, CefRefPtr<CefV8Value> object = NULL)
         : upload_total_(0),

      virtual void OnRequestComplete(CefRefPtr<CefURLRequest> request) OVERRIDE {
         CefURLRequest::Status status = request->GetRequestStatus();
         CefURLRequest::ErrorCode error_code = request->GetRequestError();
         CefRefPtr<CefResponse> response = request->GetResponse();

         //Login request
         if (request_type_ == 0)
            //download_data_ IS populating with html from the request
            object_->SetValue("newHtml", CefV8Value::CreateString(download_data_), V8_PROPERTY_ATTRIBUTE_NONE);
            //changeContent IS being called
            app_->browser_->GetMainFrame()->ExecuteJavaScript("changeContent();", app_->browser_->GetMainFrame()->GetURL(), 0);

The relevant javascript:
Code: Select all
    $(".loginBtn").click(function (e) {
        $("#loginPlaceholder").html("<img src=\"img/loading.svg\" style=\"width: 42px;\"></img>");

function changeContent()
Posts: 11
Joined: Fri Jan 22, 2016 3:48 pm

Re: Best way to pass data to javascript

Postby amaitland » Thu Jun 23, 2016 8:57 pm

If you'd provided the full example it probably would have been helpful.

Which object is your CefV8Context? To change an object outside of OnContextCreated you need to Enter and Exit a context.

Maintainer of the CefSharp project.
Posts: 874
Joined: Wed Jan 14, 2015 2:35 am

Re: Best way to pass data to javascript

Postby DJackson » Thu Jun 23, 2016 9:50 pm

That solved my problem. Thanks!
Posts: 11
Joined: Fri Jan 22, 2016 3:48 pm

Return to CEF Discussion

Who is online

Users browsing this forum: No registered users and 3 guests