VB program accessing a javascript variable

Posted on 2003-03-30
Medium Priority
Last Modified: 2013-11-13
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
<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 -->
Question by:Stanley83
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 53

Expert Comment

by:Ryan Chong
ID: 8235979
Try read the value in a Hidden Field, then vb might able to change it using the webbrowser control.
LVL 53

Expert Comment

by:Ryan Chong
ID: 8235982
>>Try read ?
Sorry, it's Try write

Author Comment

ID: 8236449
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
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!

LVL 53

Accepted Solution

Ryan Chong earned 320 total points
ID: 8236508
Try this:

in a module:

Public Enum OpenSave
    dlgOpen = 1
    dlgSave = 2
End Enum

    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
    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
            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)
            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


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

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

Hope this helps

Author Comment

ID: 8246774
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
LVL 53

Expert Comment

by:Ryan Chong
ID: 8443742
>>web chatting website ?
Try use the Winsock control

Author Comment

ID: 8444806
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..

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question