Why does this execScript code work
Hello
I have an HTML page like this:
<html>
<head>
<title>Javascript example</title>
<script language="JavaScript">
function myfunction() {
document.write("Hello World")
}
</script>
</head>
<body>
This is my static text
<form>
<INPUT NAME="submit" TYPE=Button VALUE="Click Me" onClick="myfunction()">
</form>
</body>
</html>
And IE automation code like this:
// Call a JavaScript function on page
LRESULT CMainDlg::OnScript(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL&
bHandled)
{
// Enter text in designated form and designated field
CComPtr<IDispatch> spDispDoc;
HRESULT hr=m_spBrowser->get_Document(&spDispDoc);
if (SUCCEEDED(hr))
{
// ... and query for an HTML document
CComQIPtr<IHTMLDocument2> spHTMLDoc = spDispDoc;
if (spHTMLDoc != NULL)
{
//IHTMLWindow2 parentWindow = doc.parentWindow;
CComPtr<IHTMLWindow2> spWin2;
hr=spHTMLDoc->get_parentWindow(&spWin2);
if (hr == S_OK && spWin2 != NULL)
{
CComBSTR type = L"javascript";
CComBSTR bstrScript = "myfunction";
CComVariant vRet;
hr=spWin2->execScript(bstrScript, type, &vRet);
}
}
}
return 0;
}
When the spWin2->execScript function returns, hr is 0 and vRet is VT_EMPTY
(there is no return value from javascript function. But it doesn't seem to
work in that the javascript function doesn't seem to be run. What am I
doing wrong?
Angus