Click here to Skip to main content
15,391,382 members
Articles / Desktop Programming / MFC
Posted 28 May 2002


223 bookmarked

JavaScript call from C++

Rate me:
Please Sign up or sign in to vote.
4.90/5 (62 votes)
7 Jul 2011CPOL2 min read
A class for easy implementation of JavaScript calls from C++ code.

Sample Image


Sometimes when you are using the IE Browser Control inside of a C++ application, you need to get access to the HTML elements. We can do it by using standard COM objects like IWebBrowser2, IHTMLDocument2, etc. By this method, we can easily implement features like click button, click anchor, get input string, get HTML text, etc. Unfortunately, Microsoft did not provide similar objects for JavaScript. At any case, to make control for a JavaScript object inside of an HTML page is possible by using the traditional COM approach. This article describes the class CWebPage which allows to do it and a technique to call a JavaScript function from C++ code.

How to do

As a result of using the presented class, it will be easy to call any JavaScript function from C++ code. For implementing 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 the member function CHtmlView::GetHtmlDocument(). In the case of using IWebBrowser or IWebBrowser2 components, the function get_Document will bring us the desired interface. Here is an example:

CComPtr<IDispatch> spDisp = CHtmlView::GetHtmlDocument();

The rest of the things will be done by the CWebPage class. Here is an example of a JavaScript call without parameters:


An example of a JavaScript call with two parameters will looks like:


The class implementation

class CWebPage
  virtual ~CWebPage();

  bool SetDocument(IDispatch* pDisp);
  LPDISPATCH GetHtmlDocument() const;
  const CString GetLastError() const;
  bool GetJScript(CComPtr<IDispatch>& spDisp);
  bool GetJScripts(CComPtr<IHTMLElementCollection>& spColl);
  CString ScanJScript(CString& strAText,CStringArray& args);

  bool CallJScript(const CString strFunc);
  bool CallJScript(const CString strFunc,const CString strArg1);
  bool CallJScript(const CString strFunc,const CString strArg1,
                   const CString strArg2);
  bool CallJScript(const CString strFunc,const CString strArg1,
                   const CString strArg2,const CString strArg3);
  bool CallJScript(const CString strFunc,const CStringArray& paramArray);


  CComPtr<IHTMLDocument2> m_spDoc;


Calling technique

The above mentioned technique can be split into the following steps:

  • Getting a pointer to the IHTMLDocument2 interface.
  • Getting IDispatch for the JavaScript object in the HTML document.
  • Getting the DISPID for the given name of the JavaScript function.
  • Putting the parameters to the DISPPARAM structure.
  • Calling the JavaScript function by using the Invoke method of the IDispatch interface.

Here is an example of getting an IDispatch pointer to the JavaScript objects:

bool CWebPage::GetJScript(CComPtr<IDispatch>& spDisp)
  HRESULT hr = m_spDoc->get_Script(&spDisp);
  return SUCCEEDED(hr);

And here is the final function to call JavaScript:

CComVariant CWebPage::CallJScript(const CString strFunc,
                                  const CStringArray& paramArray)
  //Getting IDispatch for Java Script objects
  CComPtr<IDispatch> spScript;
    ShowError("Cannot GetScript");
    return false;
  //Find dispid for given function in the object
  CComBSTR bstrMember(strFunc);
  DISPID dispid = NULL;
  HRESULT hr = spScript->GetIDsOfNames(IID_NULL,&bstrMember,1,
    return false;
  const int arraySize = paramArray.GetSize();
  //Putting parameters  
  DISPPARAMS dispparams;
  memset(&dispparams, 0, sizeof dispparams);
  dispparams.cArgs      = arraySize;
  dispparams.rgvarg     = new VARIANT[dispparams.cArgs];
  dispparams.cNamedArgs = 0;
  for( int i = 0; i < arraySize; i++)
    CComBSTR bstr = paramArray.GetAt(arraySize - 1 - i); // back reading
    dispparams.rgvarg[i].vt = VT_BSTR;
  EXCEPINFO excepInfo;
  memset(&excepInfo, 0, sizeof excepInfo);
  CComVariant vaResult;
  UINT nArgErr = (UINT)-1;  // initialize to invalid arg
  //Call JavaScript function         
  hr = spScript->Invoke(dispid,IID_NULL,0,
  delete [] dispparams.rgvarg;
    return false;
  return vaResult;

Notes about the demo

To call a JavaScript function from the demo, you should select a function in the tree in the left window. After this, press the "!" button on the menu bar.


  • July 07, 2011: Updated download files.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Eugene Khodakovsky
Software Developer (Senior)
United States United States
No Biography provided

Comments and Discussions

QuestionOther hill UI Pin
yaolixing4-Feb-17 20:25
Memberyaolixing4-Feb-17 20:25 
Questionwin7 Invoke call blocked, never returns Pin
Reywasawyer25-Sep-14 5:16
MemberReywasawyer25-Sep-14 5:16 
GeneralMy vote of 1 Pin
Member 1054427031-Aug-14 3:44
MemberMember 1054427031-Aug-14 3:44 
Questionimages not loading in SVG Pin
Member 1074049912-Apr-14 13:39
MemberMember 1074049912-Apr-14 13:39 
GeneralMy vote of 3 Pin
xvsdf10012-Mar-14 16:58
Memberxvsdf10012-Mar-14 16:58 
QuestionIs that possible to debug to JS function such as Multiply or Hello Pin
Ha Luong29-Sep-13 21:36
MemberHa Luong29-Sep-13 21:36 
QuestionBlank HTM page when running the application Pin
naline-annie4-Sep-13 0:08
Membernaline-annie4-Sep-13 0:08 
QuestionIs there a way to receive a callback from JavaScript? Pin
Kramer_19828-May-13 20:42
MemberKramer_19828-May-13 20:42 
QuestionNeed a HTML file for above Pin
sandeepyes31-Dec-12 22:08
Membersandeepyes31-Dec-12 22:08 
AnswerRe: Need a HTML file for above Pin
Brook Monroe28-Jan-13 4:41
professionalBrook Monroe28-Jan-13 4:41 
You'd want to create an HTML page with embedded script tags, and those script tags should contain script you'd want to call from an application hosting that web page.
GeneralVote of 5 Pin
kuzor12-Dec-12 4:30
Memberkuzor12-Dec-12 4:30 
Questionmaybe someone will be able to answer this, since the author is not really available Pin
meirs8418-Nov-12 21:15
Membermeirs8418-Nov-12 21:15 
Questioncan't find the name of the method in the java script Pin
meirs8414-Nov-12 5:25
Membermeirs8414-Nov-12 5:25 
QuestionA question about m_spDoc in CWebPage Pin
wq818313-Jun-12 16:48
Memberwq818313-Jun-12 16:48 
GeneralMy vote of 5 Pin
Member 432084415-Jul-11 12:12
MemberMember 432084415-Jul-11 12:12 
QuestionCan't find html Pin
PeterHo3866-Jul-11 21:53
MemberPeterHo3866-Jul-11 21:53 
SuggestionRe: Can't find html Pin
Eugene Khodakovsky7-Jul-11 7:37
MemberEugene Khodakovsky7-Jul-11 7:37 
GeneralRe: Can't find html Pin
PeterHo3867-Jul-11 16:01
MemberPeterHo3867-Jul-11 16:01 
GeneralHelp Me Pin
Member 288306710-Feb-11 9:50
MemberMember 288306710-Feb-11 9:50 
GeneralHelp Me Pin
Member 288306710-Feb-11 9:48
MemberMember 288306710-Feb-11 9:48 
GeneralMy vote of 5 Pin
Mukit, Ataul7-Nov-10 2:02
MemberMukit, Ataul7-Nov-10 2:02 
QuestionI am sure it is working because I already have a code similar to this, BUT Pin
aah13411-Jun-10 5:22
Memberaah13411-Jun-10 5:22 
Questionhow to pass userdefined data type to javascript function such as struct ? Pin
cdpc20-Oct-09 20:56
Membercdpc20-Oct-09 20:56 
GeneralGetIDsOfNames return Unknown name Pin
Dabara26-Aug-09 3:58
MemberDabara26-Aug-09 3:58 
QuestionWhat about cross-platform? Pin
ComaWhite8613-Dec-08 22:33
MemberComaWhite8613-Dec-08 22:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.