Thread: What is the best/easiest way to trigger a Javascript OnClick event in C++?

  1. #1
    Registered User
    Join Date
    Jun 2018
    Posts
    26

    What is the best/easiest way to trigger a Javascript OnClick event in C++?

    Hi,

    BACKGROUND

    I am developing a C++ program that retrieves the desired data from a website and exports it to Excel. This "Export to Excel" part of this task is what is giving me trouble. To perform this manually, I have to click a javascript button which triggers a certain OnClick event.


    I have been using libcurl to mimick the POST and GET requests to retrieve the data. I tried to write the retrieved data to an Excel file, instead of trying to "click" the Export to Excel button programmatically and partially succeeded. There are pages and pages of data, and only the first page is written to the Excel file by doing it this way.

    What I need to figure out is how to programmatically click this "Export to Excel" button.


    HTML SOURCE CODE

    Code:
    <!-- clientExportButton calls hiddenexportButton through ExportGrid JS method as workaround for 'wait' cursor issue on export -->
    
    
    <input id="clientExportButton" type="button" value="Export" onclick="ExportGrid('ctl02_G2_4_3_100003423_0_0_0_1_runtimeWebGrid','ctl02_G2_4_3_100003423_0_0_0_1_hiddenexportButton','ctl02_G2_4_3_100003423_0_0_0_1_outputFilterDropDownlist','50000','')" style="cursor: wait;">
    
    
    
    <input type="submit" name="ctl02$G2$4_3_100003423_0_0_0_1$hiddenexportButton" value="Export Hidden" id="ctl02_G2_4_3_100003423_0_0_0_1_hiddenexportButton" style="visibility:hidden">


    JAVASCRIPT FUNCTION

    Code:
    function ExportGrid(runtimeWebGridClientID, hiddenexportButtonClientID, outputFilterDropDownlistClientID, maxExportRowCount, message) {
    
        var filter = document.getElementById(outputFilterDropDownlistClientID);
        if (filter.value != "SELECTED" && message != "") {
    
            if (filter.value == "PAGE") {
                var grid = igtbl_getGridById(runtimeWebGridClientID);
                if (grid.Rows.length > maxExportRowCount) {
                    alert(message);
                }
            }
            else {
                alert(message);
            }
        }
    
        document.getElementById(hiddenexportButtonClientID).click();
    
        if (typeof (EnableForm) != "undefined") {
            EnableForm();
        }
    }

    What is the best/easiest way to trigger a Javascript OnClick event in C++?-capture-png

    What is the best/easiest way to trigger a Javascript OnClick event in C++?-capture2-jpg

    I have researched V8, SpiderMonkey, PhantomJS, Node.JS. What is/ which of these is the easiest, most appropriate way to accomplish what I want to do from my C++ program, and how do I use it in combination with libcurl?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > document.getElementById(hiddenexportButtonClientID ).click();
    Well one click leads to another.

    Now you need to find out what that does as well.

    You're not going to be able to call bits of JS without dragging in a lot of what a browser does for you as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2018
    Posts
    26
    Quote Originally Posted by Salem View Post
    > document.getElementById(hiddenexportButtonClientID ).click();
    Well one click leads to another.

    Now you need to find out what that does as well.

    You're not going to be able to call bits of JS without dragging in a lot of what a browser does for you as well.

    Thanks again- I was in the process of researching this and I stumbled upon this example: JavaScript function access from plain C++: An example - CodeProject

    I am in the middle of trying to incorporate that and adjust it to suit my code. I was going to use this to call the specific necessary functions called when you export the file from the browser within SelectionGrid.js , which will be saved as an HTML file in the same folder as my executable, similar to the example. I am not sure if this will work, but I am trying it regardless out of desperation.

    My compiler is flagging the WebBrowser.Iniciar(hwnd,hInst); line (see below). "Iniciar" is Spanish so I have to find the equivalent English version of this. I believe I have included the appropriate headers and such. It could be also be because the example isn't showing the initialization of "WebBrowser". This is all new to me.

    What is the best/easiest way to trigger a Javascript OnClick event in C++?-capture-jpg
    What is the best/easiest way to trigger a Javascript OnClick event in C++?-capture4-jpg
    What is the best/easiest way to trigger a Javascript OnClick event in C++?-capture5-png

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ajax response javascript=>c#=>javascript
    By xddxogm3 in forum C# Programming
    Replies: 1
    Last Post: 03-14-2012, 01:22 PM
  2. Javascript: can't detect onresize event?
    By Sebastiani in forum General Discussions
    Replies: 8
    Last Post: 12-24-2009, 08:42 PM
  3. Onclick Event(), On creating Images Componnets
    By Leite33 in forum Windows Programming
    Replies: 0
    Last Post: 10-09-2006, 03:16 PM
  4. onHover & onClick methods for a image class
    By lord mazdak in forum Windows Programming
    Replies: 8
    Last Post: 03-15-2006, 11:05 AM
  5. visual vs non and onclick
    By the-other-idiot in forum C++ Programming
    Replies: 3
    Last Post: 10-25-2005, 03:58 PM

Tags for this Thread