Click here to Skip to main content
12,693,891 members (33,723 online)
Click here to Skip to main content
Add your own
alternative version


10 bookmarked

Inject Scripts into a Page in WebBrowser Control

, 15 Mar 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Inject a script (JavaScript) into a web page loaded in a WebBrowser control.


In this tip, I will show the easiest way to inject a script (JavaScript) into a web page loaded in a WebBrowser control.


Basically, what this code does is invoke the global JavaScript eval() function, passing as parameter the script you want to invoke.

The eval() function evaluates or executes an argument. If the argument is an expression, eval() evaluates the expression. If the argument is one or more JavaScript statements, eval() executes the statements.

Using the Code

Below, you can see an implementation of a method for injecting scripts:


Public Sub InjectScript(Script As String)
    WebBrowser1.Document.InvokeScript("eval", New Object() {Script})
End Sub  


public void InjectScript(string Script)
    WebBrowser1.Document.InvokeScript("eval", new object[] { Script });

And a use example:

System.Text.StringBuilder scriptBuilder = new System.Text.StringBuilder();

scriptBuilder.AppendLine("var text = ""JavaScript injection test"";");
string script = scriptBuilder.ToString();


If the script passed as parameter in InvokeScript returns a value, this value will be returned by InvokeScript, because it's a function!

So, you can do something like this:

string script = "function() { return document.getElementById('Example').innerText; }"; 
string elementInnerText = (string)WebBrowser1.Document.InvokeScript("eval", new object[] { script });

PS.: The code above is just to illustrate the concept. I did not test.

Original Source

This was originally posted on my personal blog (in Brazilian Portuguese). However, I thought it would be interesting to write a post about it in English.

However, if you speak Portuguese, feel the urge to visit the original article:


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


About the Author

Herbert Lausmann
Brazil Brazil
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Volynsky Alex16-Mar-14 11:49
professionalVolynsky Alex16-Mar-14 11:49 
GeneralRe: My vote of 5 Pin
Herbert Lausmann16-Mar-14 11:58
professionalHerbert Lausmann16-Mar-14 11:58 
GeneralRe: My vote of 5 Pin
Volynsky Alex16-Mar-14 12:00
professionalVolynsky Alex16-Mar-14 12:00 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 16 Mar 2014
Article Copyright 2014 by Herbert Lausmann
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid