Click here to Skip to main content
Licence CPOL
First Posted 18 Jul 2010
Views 5,348
Bookmarked 1 time

Adding a Javascript Block Into a Form Hosted by WebBrowser Control

By | 18 Jul 2010 | Technical Blog
How to add a JavaScript block into a WebBrowser control
A Technical Blog article. View original blog here.[^]

Introduction

Today I found myself with a need to add a JavaScript block into a WebBrowser control in order to do some work. This post will show you the steps to do exactly that.

The Problem

In a project I'm consulting for, there was a need to dynamically add a JavaScript block into a web form that is hosted inside a WebBrowser control. So what can we do?

The Solution

We can use the Microsoft HTML Object Library to achieve the task. The Microsoft HTML Object Library is a COM library that you can reference in order to create HTML elements to use in the WebBrowser control. You first need to reference it so go to the COM tab in Add Reference view, search it and reference it. Now you can use the following code in order to add your script to the head section of the HTML:

HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement script = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement domElement = (IHTMLScriptElement)script.DomElement;
domElement.text = // put your script here;
head.AppendChild(script);

Pay attention to replace the comment with your script implementation.

Summary

Using Microsoft HTML Object Library with the WebBrowser can help you to achieve the insertion of JavaScript to a web form which is hosted inside the control. It also enables creating and appending other HTML elements and can be useful for other tasks.


License

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

About the Author

Gil Fink

Architect
Sela Group
Israel Israel

Member

Gil Fink is an expert in ASP.NET and Microsoft data platform and serves as a Senior Architect at SELA Group. He is a Microsoft data platform MVP and a certified MCPD Enterprise Application Developer. Gil has worked in the past in variety of positions and projects as a leading developer, team leader, consultant and more. His interests include Entity Framework, Enterprise Library, WCF, LINQ, ADO.NET and many other new technologies from Microsoft.
 

My technical blog: http://www.gilfink.net

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionMy Vote of 5 PinmemberUmesh. A Bhat19:55 18 Jul '11  
AnswerRe: My Vote of 5 PinmemberGil Fink20:16 18 Jul '11  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 18 Jul 2010
Article Copyright 2010 by Gil Fink
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid