6

I have a CDHTMLDialog, with which I have 2 HTML pages and a .js file with a few fairly simple functions.

I would like to be able to call one of the JS functions from my program with a simple data type passed with it. e.g. MyFunc(int). Nothing needs to be returned.

I would appreciate any guidance on how I go about this,

thanks.

Edit: Thanks to CR for his answer, and everyone else who submitted there ideas too.

Something a little like this worked in the end (stripped a little error handling from it for clarity):

void callJavaScriptFunc(int Fruit)
{
    HRESULT hRes;
    CString FuncStr;
    CString LangStr = "javascript";
    VARIANT vEmpty = {0};

    CComPtr<IHTMLDocument2> HTML2Doc;
    CComPtr<IHTMLWindow2> HTML2Wind;

    hRes = GetDHtmlDocument(&HTML2Doc);
    hRes = HTML2Doc->get_parentWindow(&HTML2Wind);

    if( Fruit > 0 ) 
    {
        FuncStr = "myFunc(808)";  // Javascript parameters can be used
        hRes = HTML2Wind->execScript(FuncStr.AllocSysString(), LangStr.AllocSysString(), &vEmpty);
    }
}
msoft
  • 579
  • 5
  • 21
Andrew
  • 1,608
  • 16
  • 31
  • do you mean jscript? How this is done depends on what javascript engine you're using: spidermonkey, v8, nitro- But you say chtml dialogue so I guess you're using jscript? – Breton Nov 03 '09 at 11:34
  • I believe it is Jscript yes. Also I have fixed my typo, it should be CDHTMLDialog. – Andrew Nov 03 '09 at 12:08
  • Just for the sake of keeping it all together, I just posted my interpretation of this on [this thread](http://stackoverflow.com/questions/2000371/return-value-from-execscriptihtmlwindow2/23319042#23319042). – c00000fd Apr 27 '14 at 03:31

3 Answers3

3

Easiest approach would be to use the execScript() method in the IHTMLWindow2 interface.

So you could get the IHTMLDocument2 interface from your CDHTMLDialog by calling GetDHtmlDocument, then get the parentWindow from IHTMLDocument2. The parent window will have the IHTMLWindow2 interface that supports execScript().

There might be an easier way to get the IHTMLWindow2 interface from your CDHTMLDialog but I'm used to working at a lower level.

CR.
  • 334
  • 2
  • 11
2

the SpiderMonkey library can "Call a JavaScript function from C++", please refer to

http://egachine.berlios.de/embedding-sm-best-practice/ar01s02.html#id2464522

but in your case, maybe this is not the answer.

whunmr
  • 2,435
  • 2
  • 22
  • 35
1

To give you a hint - javascript injection in server-side-technologies is usually performed through bulk-load at startup (GWT) or injected when the HTML is generated and served each post-back (ASP.NET). The important point of both approaches is that they inject the javascript calls somewhere in the page (or in a separated .js file linked in the HTML in case of GWT) when generating the HTML page.

Even if you're on win development (looks like it since you're on MFCs) it might be the case that you have to insert your js method call in the HTML and then load (or reload if you wish to interact with the html from your MFC app) the HTML file in your CHTMLDialog.

I don't see any other way of achieving this (maybe I am just not aware of some suitable out-of-the-box functionality) other than editing your HTML and (re)loading it - which is pretty convenient and workable if you have to call your js method once off or just inject some kind of event-handling logic. Might be a bit of a pain if you have to interact with the page from your MFC app. In this case you have to re-generate your HTML and reload it in your CHTMLDialog.

Either way you can simply have some kind of placeholder in your HTML file, look for that and replace with your javascript code, then load the page in your CHTMLDialog:

onclick="__my_Javascript_Call_HERE__"

JohnIdol
  • 48,899
  • 61
  • 158
  • 242