Hello, I would like to save the value of a Javascript-variable into a Visual-Basic-variable. How do I do this?
Code:WebBrowser.Navigate( "javascript: html = document.documentElement.outerHTML; alert(html);" )
Printable View
Hello, I would like to save the value of a Javascript-variable into a Visual-Basic-variable. How do I do this?
Code:WebBrowser.Navigate( "javascript: html = document.documentElement.outerHTML; alert(html);" )
The simplest way is to create a hidden text box on your webpage with a runat="server" in the tag. In your javascript, set the value of this textbox to the variable and then read the value of that textbox in your code behind.
Tom, I think he is writing a Winform application using the WebBrowser control.
Mike, if you just want the source code of a web page, you can simply have your webbrowser navigate to that url, then read the document.documentElement.outerHTML off your browser in DocumentCompleted event handler.
Another way to do is to use Webclient.DownloadString, which is more efficient.Code:Dim html As String = WebBrowser1.Document.DocumentElement.OuterHTML
@stanav, I really need to read the Javascript-variable. I don't just want the outerHTML. My example may be a little bit confusing.
@Tom Sawyer, but if you want to do so, you have to modify the HTML-code with Javascript and add an additional text box. No?
Yes. It's a hidden textbox, though, so it won't affect the display on the page. If you can't modify the HTML, then all you can do is grab the outerhtml and try to parse through to the variable.Quote:
Originally Posted by MikelSch
I don't think it's possible from the outside. I think you'd have to assign an object to the WebBrowser's ObjectForScripting property and then have the JavaScript code assign the value a property of your object. Of course, the Web page would have to be written to support this.
The only other option I can think of would be to use the WebBrowser's DOM, via it's Document property. That means unmanaged code and I'm still not sure it's possible. That would be the place to start researching though.
First, thanks for all the answers.
I tried to do it the Tom-Sawyer-Way with a hidden element and it worked just fine (after some hours sitting in front of the computer).
Here's my solution:
Code:Private Function GetVarFromJavaScript(ByVal strJavascript As String, ByVal strJavaScriptVariable As String)
Dim strNavigation As String = ""
Dim strReturn As String
WebBrowser.ScriptErrorsSuppressed = True
strNavigation &= "javascript:"
strNavigation &= strJavascript
strNavigation &= "if(typeof " & strJavaScriptVariable & " == ""undefined"") " & strJavaScriptVariable & " = """";"
strNavigation &= "if(typeof jsvar == ""undefined""){"
strNavigation &= "var myElement = document.createElement('<input name=""source"" id=""jsvar"" type=""hidden"" value="""">');"
strNavigation &= "myElement.value = " & strJavaScriptVariable & ";"
strNavigation &= "document.body.appendChild(myElement);"
strNavigation &= "var html = document.documentElement.outerHTML; document.write(html);"
strNavigation &= "}else{"
strNavigation &= "document.getElementById(""jsvar"").value = " & strJavaScriptVariable & ";doit();}"
WebBrowser.Navigate(strNavigation)
System.Windows.Forms.Application.DoEvents()
Try
strReturn = WebBrowser.Document.GetElementById("jsvar").DomElement.Value
Catch
strReturn = ""
End Try
Return strReturn
End Function
And here's an example how to call the function:
Code:Private Function GetHTML()
GetVarFromJavaScript("strdomain = document.domain;", "strdomain")
End Sub