1

i have written a lot of javascript functions that i want to use in my vb6 app for efficiency and time saving

is it possible to call java-script function from vb6? if possible, can you help me with some code?

2
  • Why did you write them in javascript if you need them in vb? Commented Feb 1, 2011 at 17:47
  • i once used them in JavaScript. JavaScript seems to be faster that vb for string processing, as it supports regex out of the box. in vb you have to use scripting control which has a performance problem Commented Feb 1, 2011 at 18:27

1 Answer 1

2

I hesitate to say this, but you could use the Windows Script Control ActiveX control and embed it in your VB6 application and then run your javascript code possibly with some minor adjustments, but DON'T DO IT. You might think it is efficient and time saving for you, but the reality is you will spend all sorts of extra time dealing with your "work around." Additionally, porting your code to VB6 will make it run much faster. I would only use the scripting method if you need some sort of extensibility.

Add a reference to the scripting runtime and the script control 1.0.
NOTE: in this example the variable scode is the javascript code passed to the function as a string. Since the code is simply a string you can pass in any variables you want, however, getting things back from the code is much more complex. The code can be created on the fly or retrieved from a text file.

In the example, the code is passed as a string and then the string is searched to see if it contains a function called OnProgramLoad. If it does, that function is called.

Public Sub OnProgramLoad(byval scode as string)
Dim sctest As ScriptControl

If Len(scode) < 1 Then Exit Sub

If InStr(1, scode, "OnProgramLoad", vbTextCompare) = 0 Then Exit Sub

Set sctest = New ScriptControl

With sctest
    .Language = "JScript"
    .AllowUI = True
    .AddObject "Application", App
    .AddObject "Clipboard", Clipboard
    .AddObject "Printer", Printer
    .AddObject "Screen", Screen
    .AddCode scode
    .Run "OnProgramLoad"
    End With
Set sctest = Nothing

End Sub

You would be better off porting your routines to VB6 and if you need access to a regex library in VB6 there are better ways:
http://support.microsoft.com/kb/818802

Add a reference to Microsoft VBScript Regular Expressions 5.5, then port your code...

Function TestRegExp(myPattern As String, myString As String)
   'Create objects.
   Dim objRegExp As RegExp
   Dim objMatch As Match
   Dim colMatches   As MatchCollection
   Dim RetStr As String

   ' Create a regular expression object.
   Set objRegExp = New RegExp

   'Set the pattern by using the Pattern property.
   objRegExp.Pattern = myPattern

   ' Set Case Insensitivity.
   objRegExp.IgnoreCase = True

   'Set global applicability.
   objRegExp.Global = True

   'Test whether the String can be compared.
   If (objRegExp.Test(myString) = True) Then

   'Get the matches.
    Set colMatches = objRegExp.Execute(myString)   ' Execute search.

    For Each objMatch In colMatches   ' Iterate Matches collection.
      RetStr = RetStr & "Match found at position "
      RetStr = RetStr & objMatch.FirstIndex & ". Match Value is '"
      RetStr = RetStr & objMatch.Value & "'." & vbCrLf
    Next
   Else
    RetStr = "String Matching Failed"
   End If
   TestRegExp = RetStr
End Function
Sign up to request clarification or add additional context in comments.

2 Comments

is it possible to pass argument to the javascript code? if yes, please some example will be highly appreciated
the javascript code would be a simple string, so you could pass it whatever you wanted, simply by modifying the string. see my notes in the example above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.