简体   繁体   English

JavaScript 未在 .NET WebBrowser 控件中执行

[英]JavaScript not executing in .NET WebBrowser control

Two friends of mine have tested a project I have been working on which relies on the use of the WebBrowser control, and have managed to come across a constant scenario in which the WebBrowser control is unable to execute any JavaScript on the contained web page.我的两个朋友测试了我一直从事的一个项目,该项目依赖于 WebBrowser 控件的使用,并设法遇到了一个恒定的场景,其中 WebBrowser 控件无法在包含的网页上执行任何 JavaScript。

For the sake of narrowing it down I created a new Windows Forms project that contains only a WebBrowser control, and tries to navigate to the following page: http://javatester.org/javascript.html为了缩小范围,我创建了一个仅包含 WebBrowser 控件的新 Windows 窗体项目,并尝试导航到以下页面: http : //javatester.org/javascript.html

The result on my friends computer was the below (click to enlarge):我朋友电脑上的结果如下(点击放大):

控制渲染

As you can see, it doesn't render ANYTHING when trying to call the JS specific code, alternatively if you view it in a web browser with JS disabled or enabled, you'll see something.如您所见,在尝试调用 JS 特定代码时它不会呈现任何内容,或者如果您在禁用或启用 JS 的 Web 浏览器中查看它,您会看到一些东西。

He exported his registry keys that contain the setting for JavaScript being enabled (see https://gist.github.com/4699943 ) and the value for 1400 in each of the keys matches the same settings I use, and when I run the program I have no issue.他导出了包含启用 JavaScript 设置的注册表项(请参阅https://gist.github.com/4699943 ),并且每个键中的 1400 值与我使用的相同设置相匹配,当我运行程序时我没有问题。

The code for the entire test project is as follows:整个测试项目的代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            this.webBrowser1.Navigate("http://javatester.org/javascript.html");
        }
    }
}

If a new project is created using this code that targets the 4.5 framework (either on my machine or his) it works, however if one is created using 4.0 or lower the above result occurs.如果使用此代码创建一个针对 4.5 框架的新项目(在我的机器上或他的机器上),它可以工作,但是如果使用 4.0 或更低版本创建,则会出现上述结果。 I'm not sure if this is a coincidence or if it is indeed something to do with the problem / solution.我不确定这是巧合还是确实与问题/解决方案有关。

It should be noted that if he navigates to this page in Internet Explorer there are no issues, it detects that JavaScript is enabled, this problem solely resides in this test application and the main project that it is a part of.应该注意的是,如果他在 Internet Explorer 中导航到此页面,则没有问题,它会检测到启用了 JavaScript,此问题仅存在于此测试应用程序及其所属的主项目中。

Any help would be appreciated, I've spent a good two months trying to figure this out and still can't.任何帮助将不胜感激,我花了两个月的时间试图解决这个问题,但仍然无法解决。

From my tests WebBrowser with .NET 4 reports a web browser agent string of IE 7.0.从我的测试中,带有 .NET 4 的 WebBrowser 报告了 IE 7.0 的 Web 浏览器代理字符串。 Therefore, check your JavaScript works correctly in IE7 first.因此,请先检查您的 JavaScript 在 IE7 中是否正常工作。

The scripts do not load if you are using the webbrowser control.Document Text property.如果您使用 webbrowser control.Document Text 属性,则不会加载脚本。 It perfectly fine with the Navigate method.使用 Navigate 方法完全没问题。 I need the document text property to work as i get html from third party.当我从第三方获取 html 时,我需要文档文本属性才能工作。 The same html content work perfectly fine if i create an html file out of it and open it in the regular IE browser.如果我从中创建一个 html 文件并在常规 IE 浏览器中打开它,则相同的 html 内容工作得非常好。 The webbrowser control is buggy.浏览器控件有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM