Results 1 to 2 of 2

Thread: HOWTO: Use Multiple Scripting Languages on a ASP Page and call each other's functions

  1. #1

    Thread Starter
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032

    HOWTO: Use Multiple Scripting Languages on a ASP Page and call each other's functions

    Yes, you can call a JScript Function for VBScript and vice versa. TIf you don't have Perl installed, snip out the apporpriate sections - and ASP or something tries to call a sub Write in PerlScript, so I had to stick in a dummy function to stop an error.

    Code:
    <% @LANGUAGE = "VBSCRIPT" %>
    <script language="VBScript" runat="server">
    Function testvbs()
    	Dim env
    	Response.Write "<table>"
    	Response.Write "<tr><th>From VBScript</th><td></td></tr>"
    	For Each env In Request.ServerVariables
    		Response.Write "<tr><th>" 
    		Response.Write env
    		Response.Write "</th><td>"
    		Response.Write Request.ServerVariables(env).Item
    		Response.Write "</td></tr>" & vbcrlf
    	Next
    	Response.Write "</table>"
    End Function
    </script>
    <script language="JScript" runat="server">
    function testjs() {
    	var i;
    	Response.Write("<table>");
    	Response.Write("<tr><th>From JScript</th><td></td></tr>");
    	for (i = 1; i <= Request.ServerVariables.Count; i++) {
    		Response.Write("<tr><th>"); 
    		Response.Write(Request.ServerVariables.Key(i));
    		Response.Write("</th><td>");
    		Response.Write(Request.ServerVariables(i).Item);
    		Response.Write("</td></tr>\n");
    	}
    	Response.Write("</table>");
    }
    </script>
    <script language="PerlScript" runat="server">
    use strict;
    use warnings;
    use vars qw/$Response $Request/;
    sub testps() {
    my $i = 0;
    	$Response->Write("<table>");
    	$Response->Write("<tr><th>From PerlScript</th><td></td></tr>");
    	for ($i = 1; $i <= $Request->ServerVariables->{Count}; $i++) {
    		$Response->Write("<tr><th>"); 
    		$Response->Write( $Request->ServerVariables->Key($i) );
    		$Response->Write("</th><td>");
    		$Response->Write( $Request->ServerVariables->Item($i) );
    		$Response->Write("</td></tr>\n");
    	}
    	$Response.Write("</table>");
    }
    
    #Weird hack to prevent error for main::write
    sub Write() {
    
    }
    </script>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    	"http://www.w3.org/TR/html4/loose.dtd">
    			
    <html lang="en">
    <head>
    	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    	<title>ASP ENVIRONMENT</title>
    <style type="text/css" media="all" title="Default">
    	html, body {
    		margin: 0px;
    		padding: 0px;
    	}
    
    	table {
    		margin: 10px;
    		border-collapse: collapse;
    		border: 1px solid black;
    	}
    
    	th {
    		border: 1px solid black;
    		font-family : Arial, Helvetica, sans-serif;
    		text-align : left;
    		vertical-align : middle;
    	}
    
    	td {
    		border: 1px solid black;
    		white-space : pre;
    		font-family : "Courier New", Courier, monospace;
    		text-align : left;
    		vertical-align : middle;
    	}
    </style>
    </head>
    
    <body>
    
    <p>VBScript:</p>
    <% ScriptingNamespace.testvbs %>
    
    <p>JScript:</p>
    <% ScriptingNamespace.testjs %>
    
    <p>PerlScript:</p>
    <% ScriptingNamespace.testps %>
    
    </body>
    </html>
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

  2. #2

    Thread Starter
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    I notice that you put each of the test functions in script tags and set the language there. Is there a reason for this, or can you use the @Language directive to accomplish the same thing without the script tags?
    I use the script tags because you can't change the language with the <% %> tags - all they really are is a shortcut for <script runat=server> tags using the default langauge. The @Langauge tag sets the default langauge at the page level, possibly overriding the server level default - it can only be used once, and I think before ASP processes scripts or blocks.

    When you call each function, you call it as a method of ScriptingNamespace. Is there a reason for this, or is this just clarity?
    The Scripting Language Object created has no knowledge of what's in other Scripting Langauge Objects, as far as I can tell - the ScriptingNameSpace is a COM object passed into the scripting engine after ASP creates an instance of it, along with the Response, Request, etc. I think it might even be a lowerlevel COM interface than ASP - I can find a definition of it. Anyway, VBScript would have no knowledge of a testps function in PerlScript, but the ScriptingNameSpace does. I even think the scripting engines might have to manually register functions with this object.

    Anyway, I just found out that there are activex script engines for Python, Ruby, Haskell, and Lua(?) that should be able to be used with ASP.
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

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