• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 277
  • Last Modified:

VB program accessing a javascript variable

i critically need to know how a visual basic program can retrieve a javascript variable and display it. Please help me. Thanks :)

example of a js file :

function run()
{
   i = 0;
   document.write('Value : ' + i);
}

/////////////////////////////////////

a HTML file
<html>
<head></head>
<body onload="run();">
<!-- this is the part where i want the visual basic program to change the value of i into another value after document.write has done its work -->
</body>
</html>
0
Stanley83
Asked:
Stanley83
  • 4
  • 3
1 Solution
 
Ryan ChongCommented:
Try read the value in a Hidden Field, then vb might able to change it using the webbrowser control.
0
 
Ryan ChongCommented:
>>Try read ?
Sorry, it's Try write
0
 
Stanley83Author Commented:
Sorry, i dont get what u mean. But the value captured will still be inside the memory. Thats what puzzled me. How does the VB program knows what value to capture from the memory since the javascript store the hidden value inside the computer. And also the javascript cannot have any file access. I try not to use Jscript as it is too confusing
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Ryan ChongCommented:
Try this:

in a module:

Public Enum OpenSave
    dlgOpen = 1
    dlgSave = 2
End Enum

Public Type OPENFILENAME
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    lpstrFilter As String
    lpstrCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    lpstrFile As String
    nMaxFile As Long
    lpstrFileTitle As String
    nMaxFileTitle As Long
    lpstrInitialDir As String
    lpstrTitle As String
    flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    lpstrDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
End Type

Public Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Public Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long

Public Enum dialogFlags
    cdlOFNAllowMultiselect = &H200 '512
    cdlOFNCreatePrompt = &H2000 '8192
    cdlOFNExplorer = &H80000 '524288
    cdlOFNExtensionDifferent = &H400 '1024
    cdlOFNFileMustExist = &H1000 '4096
    cdlOFNHelpButton = &H10 '16
    cdlOFNHideReadOnly = 4
    cdlOFNLongNames = &H200000 '2097152
    cdlOFNNoChangeDir = 8
    cdlOFNNoDereferenceLinks = &H100000 '1048576
    cdlOFNNoLongNames = &H40000 '262144
    cdlOFNNoReadOnlyReturn = &H8000 '32768
    cdlOFNNoValidate = &H100 '256
    cdlOFNOverwritePrompt = 2
    cdlOFNPathMustExist = &H800 '2048
    cdlOFNReadOnly = 1
    cdlOFNShareAware = &H4000 '16384
End Enum

Public Function OpenDialog(ByVal frm As Form, Optional ByVal filter As String = "All files(*.*)" & vbNullChar & "*.*", Optional ByVal Title As String = "Select a file", Optional ByVal OpenSave As OpenSave = dlgOpen, Optional ByVal DefaultDir As String = "", Optional PromptOverwrite As Boolean = True) As String
    Dim OFN As OPENFILENAME
    OFN.lStructSize = Len(OFN)
    OFN.hwndOwner = frm.hwnd
    OFN.hInstance = App.hInstance
    OFN.lpstrFilter = filter
    OFN.nFilterIndex = 1
    OFN.lpstrFile = Space$(254)
    OFN.nMaxFile = 255
    OFN.lpstrFileTitle = Space$(254)
    OFN.nMaxFileTitle = 255
    OFN.lpstrInitialDir = IIf(DefaultDir = "", CurDir, DefaultDir)
    OFN.lpstrTitle = Title
    OFN.flags = cdlOFNHideReadOnly Or IIf(PromptOverwrite = True, cdlOFNOverwritePrompt, 0)
    If OpenSave = dlgOpen Then '=1
        OpenDialog = GetOpenFileName(OFN)
        If (OpenDialog) Then
            If Dir$(Trim$(OFN.lpstrFile)) <> "" Then
                'OpenDialog = Trim$(OFN.lpstrFile)
                'OpenDialog = Left$(Trim$(OpenDialog), Len(Trim$(OpenDialog)) - 1)
                OpenDialog = Left$(OFN.lpstrFile, InStr(1, OFN.lpstrFile, Chr$(0), vbTextCompare) - 1)
            End If
        Else
            OpenDialog = "" 'Cancel was pressed
        End If
    Else ' = 2
        OpenDialog = GetSaveFileName(OFN)
        If OpenDialog > 0 Then
            'OpenDialog = Trim$(OFN.lpstrFile)
            'OpenDialog = Left$(Trim$(OpenDialog), Len(Trim$(OpenDialog)) - 1)
            OpenDialog = Left$(OFN.lpstrFile, InStr(1, OFN.lpstrFile, Chr$(0), vbTextCompare) - 1)
        Else
            OpenDialog = ""
        End If
    End If
End Function


in a form:

Option Explicit

Private Sub Command2_Click()
    Dim tmp As String
    tmp = OpenDialog(Me)
    If tmp <> "" Then WebBrowser1.Navigate tmp
   
End Sub

Private Sub WebBrowser1_StatusTextChange(ByVal Text As String)
    Dim inp As Object
    Set inp = WebBrowser1.Document.All.Item("hiddenvalue")
    MsgBox inp.Value
End Sub

test.htm:

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<form name="form1" method="post" action="">
  <input type="hidden" name="hiddenvalue" value="9732043">
  test
</form>
</body>
</html>

Hope this helps
0
 
Stanley83Author Commented:
thanx alot it can help a little..but then what im trying to get is to get the ouput to a webpage. not a msgbox. hope to make it looks like a web chatting website which can auto-refresh itself without using DHTML
0
 
Ryan ChongCommented:
>>web chatting website ?
Try use the Winsock control
0
 
Stanley83Author Commented:
hmm, its ok..thank you anyway. :) Well, i have submitted my project to my lecturer already. So, its kinda late now..haha..anyway, thanx alot for your help. Maybe next time, i will ask at an earlier time..
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now