IWebBrowser2 seemingly not executing javascript

2019-07-25 07:11发布

问题:

Users of our program occasionally have to download databases from a third-party website that requires them to log-in and specify parameters to get the correct database. I am trying to incorporate this into our program so that users don’t have to open a separate browser and also so that our program can access the database upon being downloaded.

I’ve embedded a web browser into a dialog as specified in Microsoft’s “Using MFC to Host a WebBrowser Control”, but it apparently doesn’t seem to properly execute certain website’s javascript. I have no trouble navigating the website using IE, Chrome or Firefox. However, navigating the website through the embedded browser generates multiple script errors, behaves in a completely different manner than in a stand-alone browser and ultimately, prevents the user from accessing the required database. I’ve examined the website source code and it appears that certain defaults specified within the javascript are not being accessed and certain functions are not being executed. Some of the error messages I receive are:

‘console’ is undefined ( the javascript is attempting to log messages to the console - I don’t think this has much effect on website content )

The value of the property ‘SelectSpectralModel’ is null or undefined, not a Function object 

( SelectSpectralModel is a javascript function that determines which information boxes appear, from which the user selects their desired content )

I’ve noticed that while running the code in Microsoft Visual Studio Debug mode, the following message appears frequently while trying to navigate the website:

First-chance exception ….: Microsoft C++ exception: Js::JavascriptExceptionObject at memory location…..

I enabled Exceptions in debug in an attempt to locate the error, but when the code breaks, I’m at a loss as to where the error is actually occurring.

I can’t seem to locate any information online as to why this is happening. I’ve located a lotta information about IWebBrowser2, but nothing that pertains to this particular problem. I’ve also located something about a workaround someone came up with ( for a different problem, but it seems like it might help ) using OLE Automation to run an instance of Internet Explorer, but at this point in time, I’d have to do a lot more reading to work out how this is done.

Any help I could get trying to remedy this problem or even some help in incorporating a workaround would be greatly appreciated.

回答1:

The Microsoft Embedded Web Browser control runs in a Document mode of 7. So if you were to use Fiddler to see the User-agent-string you would see the IE 7 User agent string. It uses the rendering and web standards of IE7 as well.

To override this and have it use IE11 standards you will have to create a registry entry specific to your executable.

 HKEY_LOCAL_MACHINE (or HKEY_CURRENT_USER)\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION

                 contoso.exe = (DWORD) 00009000

See the MSDN documentation on this