Results 1 to 7 of 7

Thread: [RESOLVED] How to save a Javascript-variable

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Location
    Switzerland
    Posts
    4

    Resolved [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);" )

  2. #2
    Frenzied Member
    Join Date
    May 2006
    Location
    Toronto, ON
    Posts
    1,093

    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.

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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.

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Location
    Switzerland
    Posts
    4

    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.

  5. #5
    Frenzied Member
    Join Date
    May 2006
    Location
    Toronto, ON
    Posts
    1,093

    Re: How to save a Javascript-variable

    Quote 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.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Location
    Switzerland
    Posts
    4

    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
  •  



Click Here to Expand Forum to Full Width