首先我来讲一下所以js调用ASP文件的好处:我们知道ASP之服务器端语言,我们不能使用静态页面(.htm)来运行ASP代码.这就给我们带来了一些麻烦,当我们使用静态页面而又要统计一些动态的信息时,比如文章的点击数。那么我们就可以在静态页面中使用js代码来调用ASP文件,以达到统计的功能。可以说js调用ASP文件是为静态页面准备的。
下面介绍js调用asp的方式:
你或许见过这样的调用方式:
以下是代码片段: <script language="javascript" src="xxx.js"></script> | 你也可能见过:
以下是代码片段: <!-- #include file="xxx.asp" --> | 但是下边的调用你就不一定见过了:
以下是代码片段: <script language="javascript" src="xxx.asp?borid=5&page_no=3"></script> | 是的,请相信您的眼睛,是用script的方式调用asp文件,其实这种方法网上已经运用得相当普通了,如果你玩过动网,就肯定知道它提供贴子调用的功能,想想,代码是不是类似这样子的?
以下是代码片段: <script language="javascript" src="xxx.asp?borid=5&page_no=3"></script> | 能将xxx.asp?borid=5&page_no=3的结果显示于页面,它好于
以下是代码片段: <!-- #include file="xxx.asp" --> | 的地方或许您已经看出来了,就在于可以传递参数,并显示出结果,下边举一个简单的例子(index.html):
以下是代码片段: <html> <body> <script language="javascript" src="dy.asp?num=1"></script> </body> </html> | dy.asp:
以下是代码片段: <% Dim temp Temp=request.querystring("num") If Temp=1 then Response.write "document.write(’www.mzwu.com’);" Elseif Temp=2 then Response.write "document.write(’www.163.com’);" Else Response.write "document.write(’www.sohu.com’);" End if %> | 要注意的是ASP文件的输出内容必须按照javascript规范来编写,如:
以下是代码片段: Response.write "document.write(’www.aspxhome.com’);" | 你的ASP文件输出的内容将是:
以下是代码片段: document.write(’www.aspxhome.com’); | 这样就符合javascript规范,否则结果会提示脚本错误或者一片空白! |