new String( )是学习以javascript为脚本语言的ASP程序的一个不可不了解的东西了。String的S要大写哦
看下面的例子
<%@ language=javascript %>
<html>
<head>
<title>this is a test of new string()</title>
</head>
<body>
<strong>type something and submit it</strong>
<form action="fortest3.asp" method="post">
<input type="text" name="textname"><br>
<strong> how much do you make each month<strong>
<select name="howmuch">
<option> more than 200 </option>
<option> less than 200 </option>
<option> out of work</option>
</select><br>
<input type="submit" value="OK">
</form>
</body>
</html>
fortest3.asp
<%@ language=javascript %>
<%
var textname = new String(Request.Form("textname"))
textname = textname.toUpperCase()
var howmuch = new String(Request.Form("howmuch"))
howmuch = howmuch.toLowerCase()
Response.write("<html>/r")
Response.write("you type:")
Response.write(textname+"<br>/r")
Response.write("you make each month:")
Response.write(howmuch+"<br>/r")
Response.write("</html>/r")
%>
第一个是没有asp程序的一个纯粹的html文件。但是请注意这里 <form action="fortest3.asp" method="post">这里定义了form的内容将post给fortest3.asp来处理。对于fortest3.asp,请注意这里
var textname = new String(Request.Form("textname"))
textname = textname.toUpperCase()
为什么要使用new String()呢?这就是问题的关键所在。如果我们只需要将提交的内容回传给用户,而不需要做任何的处理,那么不用new String()是完全可以的,如下例也是正确地。
<%@ language=javascript %>
<%
var textname = Request.Form("textname")
var howmuch = Request.Form("howmuch")
Response.write("<html>/r")
Response.write("you type:")
Response.write(textname+"<br>/r")
Response.write("you make each month:")
Response.write(howmuch+"<br>/r")
Response.write("</html>/r")
%>
但是如果我们想要对回传的文件做进一步的处理,例如本例中的toUpperCase(),则必须用new String()构造函数。这是因为Request.Form中的数据类型是asp的数据类型,javascript不能正常处理他。同样ASP的对象不能直接调用javascript的方法。如果用 var textname = Request.Form("textname").toUpperCase();则会得到一个错误,原因就是上述原因。但是用了new String()之后,就把asp的数据类型转化为javascript的数据类型了。也就可以用javascript的方法或者函数来处理了。
总结,new String()就是把ASP的对象转化为javascript的对象,以方便用javascript的方法和函数来处理。