JavaScript not executing in .NET WebBrowser control

user1243584 picture user1243584 · Feb 3, 2013 · Viewed 13.4k times · Source

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.

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

The result on my friends computer was the below (click to enlarge):

Control render

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.

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.

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. 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.

Any help would be appreciated, I've spent a good two months trying to figure this out and still can't.

Answer

Mister Cook picture Mister Cook · Jul 21, 2013

From my tests WebBrowser with .NET 4 reports a web browser agent string of IE 7.0. Therefore, check your JavaScript works correctly in IE7 first.