Asp JavaScript 教程 - Lesson 04 Using VBScript with JavaScriptor

 

Using VBScript with JavaScript

Get Started:

You can get JavaScript and VBScript to sing from the same sheet of music. Consider the script below.

<%@LANGUAGE="JavaScript"%>

<SCRIPT LANGUAGE="JavaScript" RUNAT="Server">
function JSGreeting()
	{
	return "Greetings from a JavaScript Function";
	}
</SCRIPT>

<SCRIPT LANGUAGE="VBScript" RUNAT="Server">
Function VBGreeting()
	VBGreeting="Greetings from a VBScript Function"
End Function

Function toDollars(x)
	toDollars=FormatCurrency(x)
End Function
</SCRIPT>

<%
var a = 2;
var b = 2;
var c = add(a,b)
c += " (Two numbers are added by JavaScript, "
c += "and then formatted into currency by VBScript.)"

function add(x,y)
	{
	result = x + y;
	result = toDollars(result);
	return result;	
	}

Response.Write("<HTML>/r")
Response.Write(JSGreeting() + "<BR>/r")
Response.Write(VBGreeting() + "<BR>/r")
Response.Write(c + " <BR>/r")
Response.Write("</HTML>/r")
%>

Click Here to run the script in a new window.

RUNAT:

Let me pick this apart from top to bottom. Directly below is a simple stand-alone JavaScript.

<SCRIPT LANGUAGE="JavaScript" RUNAT="Server">
function JSGreeting()
	{
	return "Greetings from a JavaScript Function";
	}
</SCRIPT>

This looks like any simple JavaScript you might find on the client side except for this little number: RUNAT="Server".

The RUNAT attribute tells the server to execute on the server, so the client never sees it. You can also set RUNAT="Client" in which case the Script would be passed along as-is to the web browser.

By the way, <SCRIPT LANGUAGE="JavaScript" RUNAT="Server"> should NOT be put inside ASP flags.

Take a look at the VBScript directly below.

<SCRIPT LANGUAGE="VBScript" RUNAT="Server">
Function VBGreeting()
	VBGreeting="Greetings from a VBScript Function"
End Function

This VBScript does exactly the same thing as its JavaScript counterpart. There's nothing terribly special about it.

The Invocation:

There is, however, something special about the following VBScript.

Function toDollars(x)
	toDollars=FormatCurrency(x)
End Function

The above function will take an argument from a JavaScript Function. Then it will return the appropriate value to the JavaScript function. Let me show how Function toDollars(x) is invoked.

function add(x,y)
	{
	result = x + y;
	result = toDollars(result);
	return result;	
	}

function add(x,y) is a JavaScript function. Halfway into the function, there is the following line:

result = toDollars(result);

The above line is in fact a call from a JavaScript function to invoke a VBScript function. How about that!

Warning:

Just one caveat. When you create scripts using the RUNAT attribute there is in fact an order of execution. I won't go into it now, because the order of execution is situational.

Here's what I will get into: Use scripts with the RUNAT attribute only for functions (and those global variables called inside functions). DON'T put top level code into it. You have no guarantee that top level code will execute in order using this technique.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值