Michael R wrote:
> I use mozilla to load multi frame HTML document and get the DOM built for
> it. Along with the document there are JavaScript functions loaded.
> How is it possible to call JavaScript functions from C++ ?
There are basically two options:
o) Load a javascript: URL in the relevant frame. This is easiest, but
there's not really a way to get back the result of that execution
o) The other way is to use nsIScriptContext::EvaluateStringWithValue:
static NS_DEFINE_CID(kXPCCID, NS_XPCONNECT_CID);
nsCOMPtr<nsIScriptGlobalObject> global(do_QueryInterface(aWindow));
if (!global)
return NS_ERROR_UNEXPECTED;
nsIScriptContext* context = global->GetContext();
if (!context)
return NS_ERROR_UNEXPECTED;
nsCOMPtr<nsIXPConnect> xpc(do_GetService(kXPCCID));
if (!xpc)
return NS_ERROR_UNEXPECTED;
JSContext* cx = NS_STATIC_CAST(JSContext*, context->GetNativeContext());
NS_ASSERTION(cx, "no context?");
jsval rval = JSVAL_VOID;
JSAutoTempValueRooter tvr(cx, 1, &rval);
PRBool isUndefined;
nsresult rv = context->EvaluateStringWithValue(aCode, nsnull, nsnull,
nsnull, 0, nsnull, &rval,
&isUndefined);
if (NS_FAILED(rv))
return rv;
// rval is the resulting value
Where aWindow is the frame you want to execute the code in and aCode is
the javascript code you want to execute.
(this code is for the 1.8 branch (ff 2). other versions may need some
small changes to this code)
-christian
--
All the world's a stage,
And all the men and women merely players:
They have their exits and their entrances;
And one man in his time plays many parts, [...] --W. Shakespeare
_______________________________________________
dev-tech-xpcom mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-xpcom