CoffeeCat's IT Blog

用ASP程序控制Javascript程序

有时候,我们想将ASP中的变量的值传给Javascript的程序,那怎么办呢?

很简单,只需要将Javascript的程序用ASP的response.write输出即可,举个例子

<%
    
dim x1
    x
1=1
    response.Write(
"<script>")
    response.Write(
"<!--")
    response.Write(
"var x2;")
    response.Write(
"x2 = " & x1 & ";")
    response.Write(
"-->")
    response.Write(
"</script>")
%
>

这样,就能将ASP中x1变量的值传递给Javascript程序中X2的值。


问题出现了,在<script></script>标签内,你无法使用<%%>,所以,如果你要用ASP实现与JS的变量传递,就得将整个Javascript程序用response.write输出。

因为建站需要,我写了个简单的转换程序,顺便提供给大家。注意,文件名和form的action要一致。



ASPtojavascript.asp

<%
Option Explicit

public function ASPWriter(str)
    
if str = "" then
        ASPWriter 
= ""
    
else
        
dim arr
        
        arr 
= split(str,chr(13)&chr(10))
        
dim i
        
dim temp

        
        
'双引号34,单引号39
        for i = 0 to UBound(arr)
            
if trim(arr(i)) <> "" then
                arr(i) 
= "response.Write(" & chr(34& replace(trim(arr(i)),chr(34),chr(39)) & chr(34& ")"
            
end if
            temp 
= temp & arr(i) & chr(13)
        
next
        
        ASPWriter 
= temp
    
end if
end function
%
>
<%
dim input
input 
= request.Form("input")
%
>
<body>
<center>
<font size="+5">
HTML 
- ASP输出HTML转换器
</font>
</center>
<form name="form1" method="post" action="ASPtojavascript.asp">
    
<table width="85%" align="center">
    
<tr>
        
<td>输入</td>
    
</tr>
    
<tr>
        
<td><textarea name="input" cols="120" rows="20"><%=input%></textarea></td>
    
</tr>
    
<tr>
        
<td>输出</td>
    
</tr>
    
<tr>
        
<td><textarea name="output" cols="120" rows="20"><%=ASPWriter(input)%></textarea></td>
    
</tr>
    
<tr>
        
<td align="center"><input type="submit" name="Submit" value="转 换"></td>
    
</tr>
    
</table>
</form>
</body>



输入框中输入HTML的程序,输出就是用response.write加过的ASP代码




CoffeeCat

2007年3月19日

posted on 2007-03-19 22:30 CoffeeCat 阅读(398) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
<2007年3月>
25262728123
45678910
11121314151617
18192021222324
25262728293031
1234567

导航

统计

公告

常用链接

留言簿(203)

随笔档案

收藏夹

搜索

最新评论

阅读排行榜

评论排行榜