|
-
Feb 13th, 2008, 07:54 AM
#1
Thread Starter
New Member
[RESOLVED] How to save a Javascript-variable
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);" )
-
Feb 13th, 2008, 10:00 AM
#2
Re: How to save a Javascript-variable
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.
-
Feb 13th, 2008, 12:19 PM
#3
Re: How to save a Javascript-variable
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.
Code:
Dim html As String = WebBrowser1.Document.DocumentElement.OuterHTML
Another way to do is to use Webclient.DownloadString, which is more efficient.
-
Feb 13th, 2008, 01:56 PM
#4
Thread Starter
New Member
Re: How to save a Javascript-variable
@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?
Last edited by MikelSch; Feb 13th, 2008 at 02:03 PM.
-
Feb 13th, 2008, 02:00 PM
#5
Re: How to save a Javascript-variable
 Originally Posted by MikelSch
@Tom Sawyer, but if you want to do so, you have to modify the HTML-code 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.
-
Feb 13th, 2008, 11:22 PM
#6
Re: How to save a Javascript-variable
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.
-
Feb 15th, 2008, 03:32 AM
#7
Thread Starter
New Member
Re: How to save a Javascript-variable
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
Last edited by MikelSch; Aug 30th, 2008 at 03:52 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|