ASP具备管理不同语言脚本程序的能力,能够自动调用合适的脚本引擎以解释脚本代码和执行内置函数。ASP开发环境提供了两种脚本引擎,即VBscript(缺省)和Jscript。不过,开发者并没有被限制于只能使用这两种语言,只要能够提供合适的ActiveX脚本引擎就能使用任何脚本语言。
脚本语言的选择往往基于许多不同原因:它可能是开发者最为熟悉的语言,可能是对给定工程来说能够提供最多特色支持的,也有可能是最具效率的。不同的环境和要求使得我们在选择脚本语言时注重不同的因素,同时也使得我们在某些时候面临选定的脚本语言不能直接提供其它语言固有的函数这一问题,或某个脚本已经写成但用的却是另外一种脚本语言。
此时应该怎么办?是否需要用当前所用的脚本语言重写这些脚本?或者说,是否有可能在一种脚本语言中调用其它脚本语言的内置函数?本文要说明的就是在ASP应用中如何让VBscript脚本和Jscript脚本交互以最大限度地获得两种脚本语言的特色支持。 <br><br> 一、VBscript和Jscript的内置函数 <br><br> 在VBscript和Jscript中,有大量的内置函数功能是相同或类似的。然而,在一种脚本语言中内置的函数并非总是在另外一种脚本语言中也有对应的函数。例如,VBscript提供了许多用于操作字符串和格式化数据的函数,这些函数在Jscript中并不存在。这些函数包括StrReverse()、Filter()以及FormatCurrency()等。在另一方面,Jscript所提供的用于管理数组、字符串编码等的函数在VBscript中也没有定义,如join()、reverse()、pow()、位操作、escape()和unescape()等。 <br><br> 那么,如果在Jscript程序中需要一个VBscript函数该怎么办呢? <br><br> 二、异种脚本的互相调用 <br><br> 如果需要在Jscript脚本中调用一个VBscript中内置的函数,则应该写一个VBscript用户定义函数(在这里调用VBscript内置函数),然后在Jscript脚本中象调用公用Jscript函数一样调用这个用户定义函数。 <br><br> 例如,如果要调用的VBscript内置函数是FormatCurrency(),则可以声明如下自定义函数: <br><br> < script LANGUAGE="VBscript" RUNAT="SERVER"><br> Function Formatvalue(value) <br> Formatvalue = FormatCurrency(value)<br> End Function<br> < /script><br><br> 接下来在Jscript代码中就可以象普通Jscript函数一样调用Formatvalue()了。用类似的方法也可以实现VBscript代码调用Jscript函数。 <br><br> 应用同样的规则,我们可以在任何脚本内调用任何用户定义函数。不过,从Jscript脚本内调用一个不带参数的VBscript过程(Sub)时应略加注意,此时在Jscript中应该象调用一个不带参数的Jscript函数一样调用它,如用foo()调用VBscript Sub foo过程。 <br><br> 三、数据共享 <br><br> 在某些情形下混合运用VBscript和Jscript函数是非常有用的,但在不同语言脚本之间共享数据也可能很有用。实现这种共享的方法很简单:不管使用的是什么语言,只要是在页面级声明的变量就可以任意引用。 <br><br> 对象的使用方法也相似,可以任意选用合适的语言读取、修改属性或调用对象的方法。当然给定对象的属性和方法是由创建该对象实例的语言所定义的。正如上例VBscript的过程调用,当从Jscript中调用一个不带参数的VBscript对象的方法时,其调用方法也遵从Jscript的调用规则,反之亦然。 <br><br> 四、数组管理 <br><br> 数组共享问题稍微复杂一点。虽然数组也象其它变量一样可以在不同语言脚本之间共享,但必须注意兼容方面的问题。 <br><br> VBscript数组在Jscript下可以用VBscript的符号引用,即用myArray(2)引用数组元素而不是Jscript的数组元素引用符号myArray[2>。此外,还可以使用一个特殊的Jscript对象——VBArray对象将VBscript数组转换为Jscript数组。下面的代码从VBscript数组myVBArray创建Jscript数组myjsArray: <br><br> var Temp = new VBArray(myVBArray)<br> var myjsArray<br> myjsArray = Temp.toArray()<br><br> 上述代码首先创建一个临时的VBArray对象,然后使用它的toArray()方法将自己转换为Jscript数组。此后就可以象普通Jscript数组一样使用myjsArray,如myjsArray[1>。但应当注意的是,toArray()方法将把一个多维的VBArray转换为一维的Jscript数组。 <br><br> 从VBscript中引用Jscript数组更为复杂。虽然在VBscript中我们可以直接访问Jscript数组相关的方法和属性,但没有直接访问Jscript数组的单个元素的方法。也就是说,我们可以在VBscript脚本中读取Jscript数组的长度属性,如下所示: <br><br> x = myjsArray.length <br><br> 但无法直接读取该数组的单个元素,下面的VBscript代码是不正确的: <br><br> x = myjsArray(3) <br><br> 解决该问题的一个可行的方法是执行一个转换过程,如下面的代码所示,此处假定VBscript是缺省的脚本语言: <br><br>< %<br> Dim Temp<br> Dim myVBArray<br> Temp = myjsArray.join(", ")<br> myVBArray = Split(Temp, ", ")<br>%><br><br> 此处的Jscript join()方法将数组myjsArray元素转换到一个以逗号为分割符的字符串,VBscript Split()函数将字符串转换为VBscript数组。注意这里我们是在VBscript环境下调用Jscript的join方法。依照此例,我们可以通过自定义VBscript函数来模拟Jscript的VBArray对象的toArray()方法以实现Jscript数组到VBscript数组的转换。 <br><br> 五、小结 <br><br> 在同一个ASP工程内灵活选用不同脚本语言具有许多优点,这些脚本之间的交互能力更为开发者集成不同语言所提供的内置函数和其它功能带来了更多的机会,同时也使得实现既可用于VBscript也可用于Jscript环境的通用脚本库成为可能。<br></div>
<div></div>
</div><div id="Message" class="Message"></div></font></div>
<script type="text/javascript">
document.body.oncopy = function () { setTimeout( function () { var text = clipboardData.getData("text"); if (text) { text = text + "\r\n本篇文章来源于 中国建站之家 原文链接:"+location.href; clipboardData.setData("text", text); } }, 100 ) }
</script>
<br>
<a href="javascript:window.open('http://shuqian.qq.com/post?from=3&title='+encodeURIComponent(document.title)+'&uri='+encodeURIComponent(document.location.href)+'&jumpback=2&noui=1','favit','width=930,height=470,left=50,top=50,toolbar=no,menubar=no,location=no,scrollbars=yes,status=yes,resizable=yes');void(0)" style="text-decoration:none;color:#155da5;display:block;background:url('http://shuqian.qq.com/img/add.gif') no-repeat 0px 0px;height:23px;width:300px;padding:2px 2px 0px 20px;font-size:14px;">将本文收藏到QQ书签与更多好友分享</a>
<div></div>
</td>
<table width="575" border="0" cellpadding="0" cellspacing="0">
<tr>
</tr>
<tr>
<td align="right" height=25 bgcolor=#F7F7F7 >
<script language=javascript src=/wz/sg.JS></script>
[<a href=javascript:window.print()>打 印</a>]
</tr>
</table>
<table width="575" border="0" cellpadding="0" cellspacing="0">
<tr>
</tr>
<tr>
<td align="right" height=25 bgcolor=#F7F7F7 style="display:block;padding:0px 10px">
<font color=#000000>[<script language=JavaScript src="/Article/Hits.Asp?ArticleID=5865"></script>]</font>
[<a href="javascript:history.go(-1)">返回上一页</a>]
[<a href="/user/favorite.asp?action=add&topic=ASP页面内VBScript和JScript的交互">收 藏</a>]</td>
</tr>
<tr>
<table width="575" border="0" cellpadding="0" cellspacing="0">
<tr>
</tr>
<tr>
<td style="display:block;padding:0px 10px"><div><font color=#000000>上一篇文章:</font><a href=/Article/10/130/2005/200507205864.html>在ASP中用程序控制弹出NTLM验证窗口</a></div><div><font color=#000000>下一篇文章:</font><font color=#000000><a href=/Article/10/130/2005/200507205866.html>用ASP技术实现在WEB网页上浏览目录及文件</a></font></div></td>
</tr>
</table>
<table width="575" border="0" cellpadding="0" cellspacing="0">
<tr>
<td class="titlebg1">∷相关文章评论∷ (评论内容只代表网友观点,与本站立场无关!) [<a href=/Article/comment.asp?ArticleID=5865 target=_blank>更多评论</a>...]</td>
</tr>
<tr valign="top">
<td></td>
</tr>
</table>
</td>
<td width="188" class="tableleft"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="titleback1">精彩推荐</td>
</tr>
<tr>
<td height="260" valign="center" class="showbody1"><script language=javascript src=/ad/180601.js></script></td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="titleback1">热门文章</td>
</tr>
<tr>
<td height="100" valign="top" class="showbody1"><table width="100%" border="0" cellpadding="2" cellspacing="0"><tr>
<td class="showlist11">· <a href='/Article/10/138/2005/200507256912.html' class="showlist" title="注册码大全二">注册码大全二</a></td>
<td class="showlist11"></td>
</tr><tr>
<td class="showlist12">· <a href='/Article/10/138/2005/200507256914.html' class="showlist" title="注册码大全四">注册码大全四</a></td>
<td class="showlist12"></td>
</tr><tr>
<td class="showlist11">· <a href='/Article/10/138/2005/200507256911.html' class="showlist" title="注册码大全一">注册码大全一</a></td>
<td class="showlist11"></td>
</tr><tr>
<td class="showlist12">· <a href='/Article/196/197/2005/2005081911736.html' class="showlist" title="要10G免费网络硬盘的请进来!">要10G免费网络硬盘的请进..</a></td>
<td class="showlist12"></td>
</tr><tr>
<td class="showlist11">· <a href='/Article/206/2007/2007030319347.html' class="showlist" title="通过google 赶快来赚美金">通过google 赶快来赚美金..</a></td>
<td class="showlist11"></td>
</tr><tr>
<td class="showlist12">· <a href='/Article/10/138/2005/200507256919.html' class="showlist" title="注册码大全十">注册码大全十</a></td>
<td class="showlist12"></td>
</tr><tr>
<td class="showlist11">· <a href='/Article/14/253/2005/2005092114218.html' class="showlist" title="头像-qq头像(qq新头像)4">头像-qq头像(qq新头像)4..</a></td>
<td class="showlist11"></td>
</tr><tr>
<td class="showlist12">· <a href='/Article/13/150/2006/2006022316028.html' class="showlist" title="让你轻松架设FTP服务器1">让你轻松架设FTP服务器1..</a></td>
<td class="showlist12"></td>
</tr><tr>
<td class="showlist11">· <a href='/Article/10/138/2005/200507256913.html' class="showlist" title="注册码大全三">注册码大全三</a></td>
<td class="showlist11"></td>
</tr><tr>
<td class="showlist12">· <a href='/Article/14/244/2005/2005092014121.html' class="showlist" title="梦幻背景图片7">梦幻背景图片7</a></td>
<td class="showlist12"></td>
</tr><tr>
<td class="showlist11">· <a href='/Article/14/249/2005/2005092114181.html' class="showlist" title="卡通动物图片6">卡通动物图片6</a></td>
<td class="showlist11"></td>
</tr><tr>
<td class="showlist12">· <a href='/Article/14/269/2005/2005092114241.html' class="showlist" title="网页制作素材-按钮素材2">网页制作素材-按钮素材2..</a></td>
<td class="showlist12"></td>
</tr><tr>
<td class="showlist11">· <a href='/Article/13/150/2006/2006022316032.html' class="showlist" title="让你轻松架设FTP服务器5">让你轻松架设FTP服务器5..</a></td>
<td class="showlist11"></td>
</tr><tr>
<td class="showlist12">· <a href='/Article/14/244/2005/2005092014153.html' class="showlist" title="风景图片8">风景图片8</a></td>
<td class="showlist12"></td>
</tr><tr>
<td class="showlist11">· <a href='/Article/10/138/2005/200507256918.html' class="showlist" title="注册码大全九">注册码大全九</a></td>
<td class="showlist11"></td>
</tr><tr>
<td class="showlist12">· <a href='/Article/13/150/2006/2006022316029.html' class="showlist" title="让你轻松架设FTP服务器2">让你轻松架设FTP服务器2..</a></td>
<td class="showlist12"></td>
</tr></table></td>
</tr>
<tr>
<td height="2" bgcolor="#FFFFFF"></td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="titleback1">关注此文读者还看过</td>
</tr>
<tr>
<td height="100" valign="top" class="showbody1"><table width="100%" border="0" cellpadding="2" cellspacing="0"><tr>
<td class="showlist11">· <a href='/Article/10/134/2005/2005092814400.html' target="_blank" class="showlist" title="Java 网络编程---I/O部分学习笔记整理2">Java 网络编程---I/O部分..</a></td>
<td class="showlist11"></td>
</tr><tr>
<td class="showlist12">· <a href='/Article/10/135/2005/2005102514668.html' target="_blank" class="showlist" title="CSS基础学习:样式表CSS简明教程1">CSS基础学习:样式表CSS..</a></td>
<td class="showlist12"></td>
</tr><tr>
<td class="showlist11">· <a href='/Article/11/143/2005/200507193821.html' target="_blank" class="showlist" title="摄影后期系列三:快速矫正歪斜照片(2)">摄影后期系列三:快速矫正..</a></td>
<td class="showlist11"></td>
</tr><tr>
<td class="showlist12">· <a href='/Article/10/133/2005/200507309512.html' target="_blank" class="showlist" title="PHP学习之PHP变量">PHP学习之PHP变量</a></td>
<td class="showlist12"></td>
</tr><tr>
<td class="showlist11">· <a href='/Article/10/130/2005/20050718354.html' target="_blank" class="showlist" title="关于客户端用ASP参生报表(高级篇)">关于客户端用ASP参生报表..</a></td>
<td class="showlist11"></td>
</tr><tr>
<td class="showlist12">· <a href='/Article/10/132/2005/200507192455.html' target="_blank" class="showlist" title="JSP由浅入深(11-1)">JSP由浅入深(11-1)</a></td>
<td class="showlist12"></td>
</tr><tr>
<td class="showlist11">· <a href='/Article/223/291/2007/2007111222593.html' target="_blank" class="showlist" title="博客绝不会成为最盈利Web2.0网站">博客绝不会成为最盈利We..</a></td>
<td class="showlist11"></td>
</tr><tr>
<td class="showlist12">· <a href='/Article/223/291/2007/2007030119315.html' target="_blank" class="showlist" title="eBay封杀虚拟财产交易 “淘宝”目前没有动作">eBay封杀虚拟财产交易 “..</a></td>
<td class="showlist12"></td>
</tr><tr>
<td class="showlist11">· <a href='/Article/190/192/2006/2006010715254.html' target="_blank" class="showlist" title="《信息周刊》:新应用 新体验">《信息周刊》:新应用 新..</a></td>
<td class="showlist11"></td>
</tr><tr>
<td class="showlist12">· <a href='/Article/190/194/2007/2007072821084.html' target="_blank" class="showlist" title="手把手教新站站长做Google广告的知识">手把手教新站站长做Goog..</a></td>
<td class="showlist12"></td>
</tr><tr>
<td class="showlist11">· <a href='/Article/11/143/2007/2007080121127.html' target="_blank" class="showlist" title="Photoshop利用不同素材制作意想不到的效果">Photoshop利用不同素材制..</a></td>
<td class="showlist11"></td>
</tr><tr>
<td class="showlist12">· <a href='/Article/10/130/2005/2005091713379.html' target="_blank" class="showlist" title="asp开发规范">asp开发规范</a></td>
<td class="showlist12"></td>
</tr><tr>
<td class="showlist11">· <a href='/Article/10/133/2005/200507309176.html' target="_blank" class="showlist" title="用PHP发送MIME邮件(一)">用PHP发送MIME邮件(一)..</a></td>
<td class="showlist11"></td>
</tr><tr>
<td class="showlist12">· <a href='/Article/10/131/2005/20050718950.html' target="_blank" class="showlist" title="在Asp.net中如何实现弹出提示对话框()">在Asp.net中如何实现弹出..</a></td>
<td class="showlist12"></td>
</tr><tr>
<td class="showlist11">· <a href='/Article/10/132/2005/200507192408.html' target="_blank" class="showlist" title="ASP与JSP的比较(1)">ASP与JSP的比较(1)</a></td>
<td class="showlist11"></td>
</tr><tr>
<td class="showlist12">· <a href='/Article/10/130/2005/200507205547.html' target="_blank" class="showlist" title="一个例子:从新浪提取上海天气的vbs">一个例子:从新浪提取上..</a></td>
<td class="showlist12"></td>
</tr></table></td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="titleback1">相关文章</td>
</tr>
<tr>
<td height="100" valign="top" class="showbody1">· <a href="/Article/10/130/2007/2007032719582.html" title='在ASP页里面注册DLL的VBScript CLASS'>在ASP页里面注册DLL的VBScr..</a><br>
· <a href="/Article/10/130/2005/2005101914616.html" title='WEB-ASP:返回一个ASP页面执行后的静态HTML代码'>WEB-ASP:返回一个ASP页面执..</a><br>
· <a href="/Article/10/130/2005/2005091813537.html" title='在一个ASP页面停留的多少时间的程序 '>在一个ASP页面停留的多少时..</a><br>
· <a href="/Article/10/130/2005/2005091613080.html" title='在ASP页里面注册DLL的VBScript CLASS'>在ASP页里面注册DLL的VBScr..</a><br>
· <a href="/Article/10/130/2005/2005091613033.html" title='Execel文件插入到ASP页面'>Execel文件插入到ASP页面</a><br>
· <a href="/Article/10/130/2005/2005091512740.html" title='使用索引服务器 - 创建ASP页面'>使用索引服务器 - 创建ASP页..</a><br>
· <a href="/Article/10/130/2005/200507288553.html" title='提高ASP页面的执行效率(下)'>提高ASP页面的执行效率(下..</a><br>
· <a href="/Article/10/130/2005/200507288552.html" title='提高ASP页面的执行效率(中)'>提高ASP页面的执行效率(中..</a><br>
· <a href="/Article/10/130/2005/200507288551.html" title='提高ASP页面的执行效率(上)'>提高ASP页面的执行效率(上..</a><br>
· <a href="/Article/10/130/2005/200507288471.html" title='较长text型数据无法在Asp页面中取出的解决办法'>较长text型数据无法在Asp页..</a><br>
· <a href="/Article/10/130/2005/200507288462.html" title='使用索引服务器 - 创建ASP页面'>使用索引服务器 - 创建ASP页..</a><br>
· <a href="/Article/10/130/2005/200507288461.html" title='计算ASP页面的载入时间'>计算ASP页面的载入时间</a><br>
· <a href="/Article/10/130/2005/200507288392.html" title='用模板建立动态ASP页'>用模板建立动态ASP页</a><br>
· <a href="/Article/10/130/2005/200507267314.html" title='[ASP-Last-Code]显示ASP页面的代码'>[ASP-Last-Code]显示ASP页面..</a><br>
· <a href="/Article/10/130/2005/200507257003.html" title='使用InterDev调试ASP页面及用户自定义DLL'>使用InterDev调试ASP页面及..</a><br>
· <a href="/Article/10/130/2005/200507205715.html" title='在一个ASP页面停留的多少时间的程序'>在一个ASP页面停留的多少时..</a><br></td>
</tr>
</table>
</td>
</tr>
</table>
<table width="778" border="0" align="center" cellpadding="0" cellspacing="0" class="tableborder">
<tr>
<td height="3"></td>
</tr>
</table>
<table width="778" border="0" align="center" cellpadding="0" cellspacing="0" class="tableborder">
<tr>
<td height="23" align="right">
<table width="100%" border="0" cellspacing="0" class="tablebody8" cellpadding="0">
<tr>
<td height="5"></td>
</tr>
<tr>
<td height="23" align="right" valign="middle"><center><a href="/support/about.asp" class="navmenu2">关于本站</a> -
<a href="/support/help.asp" class="navmenu2">网站帮助</a> -
<a href="/support/advertise.asp" class="navmenu2">广告合作</a> -
<a href="/support/declare.asp" class="navmenu2">下载声明</a> -
<a href="/link/" target="_blank" class="navmenu2">友情连接</a> -
<a href="/support/sitemap.asp" class="navmenu2">网站地图</a> -
<a href="#" target="_blank" class="navmenu2">人才招聘</a>
</tr>
</table></td>
</tr>
<tr>
<td height="1"></td>
</tr>
</table>
<table width="778" border="0" align="center" cellpadding="0" cellspacing="0" class="tableborder">
<tr>
<td height="60" align="center" class="tablebody1">网站合作、内容监督、商务咨询:QQ: 9576619
<br>
Copyright ? 2005--2008 中国建站之家版权所有
<br><a href="http://www.miibeian.gov.cn" target="_blank" title="粤ICP备05092265号"><font color=#000000>粤ICP备05092265号
</font><br><script src='http://s6.cnzz.com/stat.php?id=44148&web_id=44148&show=pic' language='JavaScript' charset='gb2312'></script> </td>
<script language="javascript" src="/inc/Std_StranJF.Js"></script>
</table>
</body>
</html>
<span id="naruco_ad_body" style="display:none;">
<script language=javascript src=/adfile/top.js></script>
</span>
<script type="text/javascript">
var naruco_ad = document.getElementById('naruco_ad');
if (naruco_ad != null) {
naruco_ad.innerHTML=naruco_ad_body.innerHTML;
naruco_ad_body.innerHTML="";
}
</script>