Several methods of VC calling JavaScript (recommended) _javascript tips

Source: Internet
Author: User

First: called by Execscript. This method, while convenient to operate, cannot get the return value.

M_sphtmldoc->get_parentwindow (&m_phtmlwindow);
VARIANT ret;
RET.VT = Vt_empty;
BSTR BSTR = sscript.allocsysstring ();
BRet = M_phtmlwindow->execscript (BSTR, L "JavaScript", &ret);:
: SysFreeString (BSTR);
Sret = CString (ret);

The second: first use GetIDsOfNames to find the script function name, and then call. This method can return the result with a return value. However, you cannot call JS's system functions, such as Eval.

BOOL cdhtmldlgwindow::calljscript (const CString strfunc, const cstringarray& ParamArray, ccomvariant* pvarresult) {
  CComPtr Spscript;
  if (Null==m_sphtmldoc) {return FALSE;
  HRESULT hr;
  hr = M_sphtmldoc->get_script (&spscript); if (!
  SUCCEEDED (HR)) {return FALSE;
  } CComBSTR Bstrmember (Strfunc);
  DISPID DISPID = NULL;
  hr = Spscript->getidsofnames (iid_null,&bstrmember,1, locale_system_default,&dispid);
  if (FAILED (HR)) {return FALSE;

  const int arraysize = Paramarray.getsize ();
  Dispparams Dispparams;
  memset (&dispparams, 0, sizeof dispparams);
  Dispparams.cargs = ArraySize;
  
  Dispparams.rgvarg = new Variant[dispparams.cargs]; for (int i = 0; i < arraysize i++) {CComBSTR BSTR = Paramarray.getat (ArraySize-1-i);//Back reading BS Tr.
    CopyTo (&dispparams.rgvarg[i].bstrval);
  DISPPARAMS.RGVARG[I].VT = VT_BSTR;

  } Dispparams.cnamedargs = 0;
  Excepinfo Excepinfo;memset (&excepinfo, 0, sizeof excepinfo);
  CComVariant Varesult; UINT Nargerr = (UINT)-1; Initialize to invalid arg hr = Spscript->invoke (dispid,iid_null,0, Dispatch_method,&dispparam

  S,&AMP;VARESULT,&AMP;EXCEPINFO,&AMP;NARGERR);
  delete [] dispparams.rgvarg;
  if (FAILED (HR)) {return FALSE;
  } *pvarresult = Varesult;
return TRUE; }

When actually used, it is possible to first access (Navigate) a page. Then, in VC inside the page for some JS call, and retrieve the results. It is possible that this JS call function is not in this page. It is generally possible to call an eval (some JS statements) in the form of a function that is not in the page, but neither of the above methods supports eval.

Third: Get the current document context through IScriptControl, and then call the Iscriptcontrol::raw_eval operation. (Only with Raw_eval, the Eval method prompts for no permissions.) )

First define a: Iscriptcontrolptr, and then in OnDocumentComplete inside call M_sphtmldoc->get_parentwindow (&m_phtmlwindow);

Iscriptcontrolptr->addobject ("Window", M_phtmlwindow, VARIANT_TRUE);

The third requires the import of "MSScript.ocx"

The above VC call JavaScript several methods (recommended) is small to share all the content of everyone, hope to give you a reference, but also hope that we support cloud habitat community.

Related Article

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.