'JSON' is undefined

2020-03-26 03:55发布

问题:

I'm trying to load the Chromecast background website into a c# WebBrowser but am getting:

I assumed it was happening because the webbrowser is using IE7 by default(?) which might not play well with the JS in the website. I tried to updated the registery FEATURE_BROWSER_EMULATION to 9000 hoping force the webbrowser to use IE9 framework. But I still get the same errors.

Is the webbrowser to basic for this task or is there a way around this problem?

EDIT:

So when I print the webbrowser.version I get: Version: 11.0.9600.16518. Which is the current version of IE I have. If I open IE11 and navigate to the url its works great. Not sure why the webbroswer is having an issue.

I tried to inject a JSON Parser into the webpage with this code:

HtmlDocument doc = webBrowser1.Document;
            Console.WriteLine(doc);
            HtmlElement head = doc.GetElementsByTagName("head")[0];
            HtmlElement s = doc.CreateElement("script");
            s.SetAttribute("type", "text/javascript");
            s.SetAttribute("src", "http://192.168.1.23:10000/JSON-js-master/json2.js");
            head.AppendChild(s);

I tried with local and external ips referencing the host file. Didn't seem the make a difference.

回答1:

Apparently when the WebBrowser control runs, it runs in in the version of IE installed on the computer, but it runs in IE7 compatibility mode. Trying to load the link you listed requires a more modern browser. As a matter of fact, the page you requested needs IE10 or higher.

You do need to change the browser emulation settings in the registry so that the WebBrowser control users a more modern IE feature set:

Internet Feature Controls

As you already said in your question, the two areas in the registry that need to be adjusted are:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION

In each of those two locations, the process name of your applications is included with a DWORD value set indicating the level of compatibility. The specific values are found on the Internet Feature Controls help page. The names of the file is the name of you application "MyApplication1.exe", or if you're running from within Visual Studio, it is "MyApplication1.vshost.exe".

Because I have IE 11 installed, I set the browser emulation setting to 11001 and it worked for your linked webpage.