Results 1 to 7 of 7

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

Threaded View

  1. #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