简体   繁体   中英

running javascript code on page inside a WebBrowser control

I have a web page that is loaded inside a WebBrowser control,the HTML is something like:

<input type='text' id='baa' name='baa' >

I want to change value of baa input HTML element using javacript code, I did it:

webBrowser1.Navigate("javascript:document.getElementById('baa').value = 'baa'; void(0)");

but it does not works in IE. How to fix this? Thanks in advance.

if you want to run script, you are better off using .execScript which comes with pre.NET and .NET WB control, or .InvokeScript, which comes with .NET WB control only - using these are a much better idea as you can use VBScript as well as JavaScript (JScript).

To change the value, all you need to do is the following, assuming your WebBrowser control name is wbMain. This is in VB but doing it in C# is no difference except for language syntax, which is easy to read and reuse in whatever language you want.

Dim hDoc as MSHTML.IHTMLDocument ' declare the html document object.

Set hDoc = wbMain.Document ' Get a reference to the WB controls document object.

hDoc.getElementById("baa").value = "baa"

In fact, you dont even need to get a reference or do any of the above, if you want, you can use it directly off the WB control.

wbMain.Document.getElementById("baa").value = "baa"

If you want to change it to something else, such as "naa" you would just do this:

wbMain.Document.getElementById("baa").value = "naa"

Let me know if this is helpful to you, or if you need more help.

Cheers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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