views:

321

answers:

3

Hello, 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:

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)";
     hRes = HTML2Wind->execScript(FuncStr.AllocSysString(), LangStr.AllocSysString(), &vEmpty);
    }
}
+1  A: 

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
+3  A: 

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
Thanks, this worked great. I will post a code snippet in my question so people can see my rough solution. Thanks again.
YoungPony
nice solution - I didn't know about this approach
JohnIdol
+2  A: 

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