I search on internet 1 week regularly no solution. I found one http://www.codeproject.com/Articles/2352/JavaScript-call-from-C. But that solution is very difficult to understand.
This program can call JavaScript but online. I just want to call JavaScript Internally. For example test.htm inside the project. Just 1 simple function of JavaScript and 1 simple function of c++. Please Help.
According to my project any simple function. For example adding from JavaScript or multiplication from c++ etc.
This is the way to call javascript function from MFC application
Suppose this is a javascript function in your html file.
JAVSCRIPT function
There may be different type of javascript function
Type 1: No arguments, No return Value
function myfunction()
{
Alert('Hey I am Here');
}
Type 2: having Arguments with No return Value
function myfunction1(value)
{
Alert(value);
}
Type 3: Arguments may or may not have with return Value
function myfunction2(value)
{
return value;
}
This function is used to call javascript function
BOOL CMyDlg::CallClientScript(LPCTSTR pStrFuncName, CStringArray* pArrFuncArgs, CComVariant* pOutVarRes)
{
BOOL bRes = FALSE;
CComVariant vaResult;
CComPtr<IHTMLDocument2> pIDoc2;
if(SUCCEEDED(this->GetDHtmlDocument(&pIDoc2))) //Uses CDHtmlDialog as 'this'
{
//Getting IDispatch for Java Script objects
CComPtr<IDispatch> spScript;
if(SUCCEEDED(pIDoc2->get_Script(&spScript)))
{
//Find dispid for given function in the object
CComBSTR bstrMember(pStrFuncName);
DISPID dispid = NULL;
if(SUCCEEDED(spScript->GetIDsOfNames(IID_NULL, &bstrMember, 1, LOCALE_USER_DEFAULT, &dispid)))
{
const int arraySize = pArrFuncArgs ? pArrFuncArgs->GetSize() : 0;
//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 = pArrFuncArgs->GetAt(arraySize - 1 - i); // back reading
bstr.CopyTo(&dispparams.rgvarg[i].bstrVal);
dispparams.rgvarg[i].vt = VT_BSTR;
}
EXCEPINFO excepInfo;
memset(&excepInfo, 0, sizeof excepInfo);
UINT nArgErr = (UINT)-1; // initialize to invalid arg
//Call JavaScript function
if(SUCCEEDED(spScript->Invoke(dispid, IID_NULL, 0, DISPATCH_METHOD, &dispparams, &vaResult, &excepInfo, &nArgErr)))
{
//Done!
bRes = TRUE;
}
//Free mem
delete [] dispparams.rgvarg;
}
}
}
if(pOutVarRes)
*pOutVarRes = vaResult;
return bRes;
}
How to use?
On click of any button you can call this function, for example
onbuttonclickOk()
{
CStringArray arrArgs;
CComVariant varRes;
//arrArgs.Add(_T("1"));//you can add value to CStringArray
//arrArgs.Add(_T("2"));//if your javascript function having any arguments
CallClientScript(L"myfunction",&arrArgs,&varRes);
}
How to get return value of javaScript function?
If your javascript function is returning any value, it will be stored invarRes
variable.
you can get return value like
CString strTmp;
if(CallClientScript(L"myfunction3",&arrArgs,&varRes))
{
if(varRes.vt == VT_BSTR){
strTmp = varRes.bstrVal;// return value of javascript function.
}
}
EDIT:
Work with edit control
suppose you have an edit control like this
<input type="text" id="MyEdit" />
In DoDataExchange add one line
void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
CDHtmlDialog::DoDataExchange(pDX);
DDX_DHtml_ElementInnerText(pDX, _T("MyEdit"), m_strMyEdit);// add this line
}
m_strMyEdit
is a CString
variable you can declare in your .h
file.
OnInitDialog, you can give any value to show in edit control, like:
m_strMyEdit = _T("Enter anything");
And if you want to update editbox value later in your program just add these lines
UpdateData();
m_strMyEdit =_T("Hello World");// give new value
UpdateData(FALSE);
if you want to access editbox value you can directly use m_strMyEdit
variable.
I think this will help you..!!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With