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



.



Relevant Pages

  • Re: CHtmlView document.returnValue
    ... It just calls a JavaScript function which returns a double value, ... Once you have the IHTMLDocument2 interface you can access the Script ... // Access the IHTMLDocument2 interface from the HTML view ... // Given the IHTMLDocument2, access the Script Engine ...
    (microsoft.public.vc.mfc)
  • Why does this execScript code work
    ... I have an HTML page like this: ... // Enter text in designated form and designated field ... CComVariant vRet; ... (there is no return value from javascript function. ...
    (microsoft.public.vc.atl)
  • JavaScript From PageLoad()?
    ... I wish to call a JavaScript function from a .js file ... body.Attributes.Add("onload", temp); ... catch (NullReferenceException x) ... The rendered html contains: ...
    (microsoft.public.vstudio.general)
  • JavaScript From PageLoad()?
    ... I wish to call a JavaScript function from a .js file ... body.Attributes.Add("onload", temp); ... catch (NullReferenceException x) ... The rendered html contains: ...
    (microsoft.public.dotnet.framework.aspnet)
  • JavaScript From PageLoad()?
    ... I wish to call a JavaScript function from a .js file ... body.Attributes.Add("onload", temp); ... catch (NullReferenceException x) ... The rendered html contains: ...
    (microsoft.public.vstudio.development)