user1553136
user1553136

Reputation: 328

Using VBScript function in JScript when Language=VBScript

I know it is possible to use functions coded in VBScript if language="JScript" as follows :

<%@ language="JScript" %>
<% Response.Write(myFunc()); %>
<script runat="server" language="vbscript">
    Function myFunc
        myFunc="test"
    End Function
</script>

However, before refactoring a script in JScript, I wanted to know if, assuming the language is set to be VBScript, is there still a possibility to use VBScript functions in the JScript. Or, in that case, only the other way aroud is possible.

Thanks!

Upvotes: 1

Views: 1383

Answers (1)

Erik Oosterwaal
Erik Oosterwaal

Reputation: 4384

I have tested this before using jscript, vbscript and Python. It is possible to call a function from one language in a block of code from another and work with the results.

There are some caveats though:

You need to make sure the function returns a type that the other language can recognize; so returning Python objects to vbscript will not work, but if you use simple types like strings, numbers and booleans it is possible. I think even arrays work between vbscript and jscript.

The second thing to consider is that there is a sequence in the order in which the scripts are processed on the server; I don't know exactly anymore what it is, but ASP will first process one language, and after that the other.
This can lead to strange things happening when your code calls a function in a different language and that function can't be found because ASP needs to still process the language for that function.

As long as you keep your execution in functions and make sure you only write code outside of these functions in one specific language, this shouldn't be a problem.

Here's more info on the order of execution: http://www.kidslovepc.com/asp/order_execution.shtml

Upvotes: 2

Related Questions