<script>ec(2);</script> <span class="bold">Javascript编写Asp时需要注意的一些地方</span> 论坛里面有不少人在使用Javascript编写Asp,经常有人在论坛提问,为什么Asp对象在对比指定值时返回结果不对?现在在这里给大家写点关于使用Javascript编写Asp一些需要注意的地方。 最常见的问题: <code><span style="color: #000000"> <span style="color: #0000bb">Response</span><span style="color: #007700">.</span><span style="color: #0000bb">Write</span><span style="color: #007700">(</span><span style="color: #0000bb">Request</span><span style="color: #007700">.</span><span style="color: #0000bb">Form</span><span style="color: #007700">(</span><span style="color: #dd0000">"Key"</span><span style="color: #007700">) == </span><span style="color: #dd0000">""</span><span style="color: #007700">) </span> </span> </code> 返回的结果怎么都是"False"。在这里,我们使用typeof就可以发现:Request.Form("Key")返回的其实是一个object类型对象,而不是最终的值。所以,我们需要取出最终的值才能够做出正确的判断。可以使用如下解决方法: <code><span style="color: #000000"> <span style="color: #007700">var </span><span style="color: #0000bb">Nothing</span><span style="color: #007700">; </span><span style="color: #ff8000">// 兼容不存在undefined的老版本jscript </span><span style="color: #0000bb">Response</span><span style="color: #007700">.</span><span style="color: #0000bb">Write</span><span style="color: #007700">(</span><span style="color: #0000bb">Request</span><span style="color: #007700">.</span><span style="color: #0000bb">Form</span><span style="color: #007700">(</span><span style="color: #dd0000">"Key"</span><span style="color: #007700">).</span><span style="color: #0000bb">Item </span><span style="color: #007700">== </span><span style="color: #0000bb">Nothing</span><span style="color: #007700">); </span><span style="color: #0000bb">Response</span><span style="color: #007700">.</span><span style="color: #0000bb">Write</span><span style="color: #007700">(</span><span style="color: #0000bb">Request</span><span style="color: #007700">.</span><span style="color: #0000bb">Form</span><span style="color: #007700">(</span><span style="color: #dd0000">"Key"</span><span style="color: #007700">).</span><span style="color: #0000bb">Item </span><span style="color: #007700">=== </span><span style="color: #0000bb">Nothing</span><span style="color: #007700">); </span><span style="color: #ff8000">// IE 5.5 之后undefined已经是一个常量,可直接访问(JScript版本是跟随IE升级的) </span><span style="color: #0000bb">Response</span><span style="color: #007700">.</span><span style="color: #0000bb">Write</span><span style="color: #007700">(</span><span style="color: #0000bb">Request</span><span style="color: #007700">.</span><span style="color: #0000bb">Form</span><span style="color: #007700">(</span><span style="color: #dd0000">"Key"</span><span style="color: #007700">).</span><span style="color: #0000bb">Item </span><span style="color: #007700">== </span><span style="color: #0000bb">undefined</span><span style="color: #007700">); </span><span style="color: #0000bb">Response</span><span style="color: #007700">.</span><span style="color: #0000bb">Write</span><span style="color: #007700">(</span><span style="color: #0000bb">Request</span><span style="color: #007700">.</span><span style="color: #0000bb">Form</span><span style="color: #007700">(</span><span style="color: #dd0000">"Key"</span><span style="color: #007700">).</span><span style="color: #0000bb">Item </span><span style="color: #007700">=== </span><span style="color: #0000bb">undefined</span><span style="color: #007700">); </span><span style="color: #0000bb">Response</span><span style="color: #007700">.</span><span style="color: #0000bb">Write</span><span style="color: #007700">(</span><span style="color: #0000bb">Request</span><span style="color: #007700">.</span><span style="color: #0000bb">Form</span><span style="color: #007700">(</span><span style="color: #dd0000">"Key"</span><span style="color: #007700">).</span><span style="color: #0000bb">Item </span><span style="color: #007700">== </span><span style="color: #0000bb">null</span><span style="color: #007700">); </span> </span> </code> 所以,取值时我们如果使用完整的取值方式,就不会出现那些奇怪的现象。 下面说一些Asp中取值的例子: QUOTE: 例:Request.Form("Key") 取值:Request.Form.Item("Key").Item 或:Request.Form("Key").Item 注意这里的Request.Form.Item,直接当属性访问时返回的是一个字符串对象(值类型),作为一个js的“function”使用时返回的是一个object。 Request.Cookies、Request.QueryString、Request.ServerVariables的取值写法同上。 vbs中的简写Request("Key")在javascript中,对应的是Request.Item("Key") QUOTE: 例:rs("FiledName") 取值:rs.Fields.Item("FiledName").Value 或:rs.Fields("FiledName").Value 或:rs("FiledName").Value 下面说一下Session和Application。 我们常用的Session("Key"),返回的已经是最终值了,所以这个方法可以放心使用。顺便也提一下,Session的完整写法应该是: Session.Contents.Item("Key") 也可以写成 Session.Contents("Key") Contents对象好像有点多余,其实并非如此,某些时候我们还是需要用到它的,主要是用于Session的枚举和Session的Remove操作。如: <code><span style="color: #000000"> <span style="color: #0000bb">Session</span><span style="color: #007700">.</span><span style="color: #0000bb">Contents</span><span style="color: #007700">.</span><span style="color: #0000bb">Remove</span><span style="color: #007700">(</span><span style="color: #dd0000">"Key"</span><span style="color: #007700">); </span><span style="color: #0000bb">Session</span><span style="color: #007700">.</span><span style="color: #0000bb">Contents</span><span style="color: #007700">.</span><span style="color: #0000bb">RemoveAll</span><span style="color: #007700">() </span> </span> </code> Abandon方法不属于Contents,它是直接属于Session的一个方法,使用时需要注意这个。 Application同样也存在Contents对象,用法和Session相同。 下面说一下Js枚举Asp对象。 我们有时可能想知道,客户端到底提交了一些什么数据?Application或Session中存在一些什么数据? 在js中,我们通常使用for...in方式获取一个对象的所有属性,但是,在Asp中,这个方式对于Asp对象就无能为力了。怎么办呢?这个时候,我们可以使用枚举(Enumerator)和VBArray来获取: <code><span style="color: #000000"> <span style="color: #007700">var </span><span style="color: #0000bb">app </span><span style="color: #007700">= new </span><span style="color: #0000bb">Enumerator</span><span style="color: #007700">(</span><span style="color: #0000bb">Application</span><span style="color: #007700">.</span><span style="color: #0000bb">Contents</span><span style="color: #007700">); var </span><span style="color: #0000bb">arr </span><span style="color: #007700">= new Array; while(!</span><span style="color: #0000bb">app</span><span style="color: #007700">.</span><span style="color: #0000bb">atEnd</span><span style="color: #007700">()) { var </span><span style="color: #0000bb">value </span><span style="color: #007700">= </span><span style="color: #0000bb">Application</span><span style="color: #007700">.</span><span style="color: #0000bb">Contents</span><span style="color: #007700">.</span><span style="color: #0000bb">Item</span><span style="color: #007700">(</span><span style="color: #0000bb">app</span><span style="color: #007700">.</span><span style="color: #0000bb">item</span><span style="color: #007700">()); if(</span><span style="color: #0000bb">typeof value </span><span style="color: #007700">== </span><span style="color: #dd0000">"unknown"</span><span style="color: #007700">) </span><span style="color: #0000bb">value </span><span style="color: #007700">= (new </span><span style="color: #0000bb">VBArray</span><span style="color: #007700">(</span><span style="color: #0000bb">value</span><span style="color: #007700">)).</span><span style="color: #0000bb">toArray</span><span style="color: #007700">() </span><span style="color: #dd0000">" (VBArray)"</span><span style="color: #007700">; else </span><span style="color: #0000bb">value </span><span style="color: #007700"> = </span><span style="color: #dd0000">" (" </span><span style="color: #007700"> </span><span style="color: #0000bb">typeof value </span><span style="color: #007700"> </span><span style="color: #dd0000">")"</span><span style="color: #007700">; </span><span style="color: #0000bb">arr</span><span style="color: #007700">.</span><span style="color: #0000bb">push</span><span style="color: #007700">(</span><span style="color: #0000bb">app</span><span style="color: #007700">.</span><span style="color: #0000bb">item</span><span style="color: #007700">() </span><span style="color: #dd0000">" : " </span><span style="color: #007700"> </span><span style="color: #0000bb">value</span><span style="color: #007700">); </span><span style="color: #0000bb">app</span><span style="color: #007700">.</span><span style="color: #0000bb">moveNext</span><span style="color: #007700">(); } </span><span style="color: #0000bb">Response</span><span style="color: #007700">.</span><span style="color: #0000bb">Write</span><span style="color: #007700">(</span><span style="color: #0000bb">arr</span><span style="color: #007700">.</span><span style="color: #0000bb">join</span><span style="color: #007700">(</span><span style="color: #dd0000">"<br>"</span><span style="color: #007700">)); </span> </span> </code> 就写这些了,希望对刚刚开始学使用js编写asp的朋友们有些帮助。
|