Click here to Skip to main content
16,012,223 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have HTML based reports that get fed to the WebBrowser control that needs to use javascript.

My issue is that my javascript won't execute when using WebBrowser.DocumentText but, executes fine when using WebBrowser.Navigate.

Does anyone know why this would happen?

Thanks

Example HTML:
XML
<html>
<head>
<title></title>
</head>
<body>
<a href="#" onClick="alert('yeah');">Click Me</a>
</body>
</html>
Posted
Updated 22-Jun-11 7:37am
v2
Comments
thatraja 22-Jun-11 14:22pm    
Not sure.
Try onClick="javascrpt:alert('yeah');"

1 solution

Must be something else you're doing ( wrong maybe ;) ). I threw toghether this small example and it works exactly as expected:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

        private void button1_Click(object sender, EventArgs e)
        {
            webBrowser1.DocumentText = "<html><head><title></title></head><body><a href=\"#\" onClick=\"alert('yeah');\">Click Me</a></body></html>";
        }
    }
}


A simple form with a WebBrowser control on it and a button that makes the control load the HTML you provided via DocumentText. The form shows the ClickMe link and when I click it the alert box springs up with "yeah".

Best Regards,

--MRB
 
Share this answer
 
Comments
andre96dre 22-Jun-11 18:27pm    
Thanks for the quick reply. It turns out that my IE was somehow corrupted and I believe the browser control uses the current version of IE that's installed on the pc. I tested the above code on another pc and it works fine. I thought I was going crazy.
Manfred Rudolf Bihy 24-Jun-11 6:08am    
You're correct! The WebBrowser control does indeed use the IE that's installed on your PC.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900