Asp JavaScript 教程 - Lesson 03 The new String( ) Constructor

 

The new String( ) constructor

Get Started:

new String( ) is another strange place to go early on the lesson plan. But just like Escape Characters, new String ( ) is vital to successful ASP JavaScript applications. Below are two scripts for Lesson 03.

<%@LANGUAGE="JavaScript"%>
<%
//No ASP Here, just a regular HTML Page
%>
<HTML>
<STRONG>Type something into the text box and submit it.</STRONG>
<FORM ACTION="script03a.asp" METHOD="Post">
<INPUT TYPE="Text" NAME="WebPageVariable"><BR>
<STRONG>How Much Money do you make each month?</STRONG><BR>
<SELECT NAME="monthlySalary">
<OPTION>Under $5,000,000</OPTION>
<OPTION>Above $5,000,000</OPTION>
<OPTION>Nobody's darn business</OPTION>
</SELECT><BR>
<INPUT TYPE="Submit" VALUE="Submit">
</FORM>
</HTML>

Click Here to run the script in a new window.

Below is the actual ASP Script that does the heavy lifting.

 

<%@LANGUAGE="JavaScript"%>
<%
var WebPageVariable = new String( Request.Form("WebPageVariable") )
WebPageVariable = WebPageVariable.toUpperCase();

var monthlySalary = new String( Request.Form("monthlySalary") )
monthlySalary = monthlySalary.toLowerCase();

Response.Write("<HTML>/r");
Response.Write("The Web Page Variable you typed is: ");
Response.Write(WebPageVariable + "<BR>/r");
Response.Write("The monthly salary you listed is: " + monthlySalary + "<BR>/r");
Response.Write("</HTML>/r");
%>

 

new String( ) in Action:

Now check out the ASP lines below.

var monthlySalary = new String( Request.Form("monthlySalary") )
monthlySalary = monthlySalary.toLowerCase();

Request.Form gets its own lesson later on. Here's the point of showing it to you now. The data held in Request.Form (which ultimately comes from the user) is not a JavaScript data type. Instead, it is a native ASP data type. JavaScript cannot manipulate ASP data types.

The solution is cast the ASP data type into a JavaScript data type. In this case, the way to do that is the new String( ) constructor.

Quick Note:

If all you want to do is redirect the text from Request.Form back out to the user, then you don't need new String( ). But if you need to act upon the text with a JavaScript Function or Method, then you will need new String( ).

Without new String( ):

Take a look at the line I printed below.

var WebPageVariable = Request.Form("WebPageVaraible").toUpperCase(); 

ERROR! The Line Above Will Throw An Error because Request.Form("WebPageVaraible") is an ASP Object, and toUpperCase( ) is a JavaScript method.

Microsoft JScript runtime error '800a01b6'

Object doesn't support this property or method

/asp/Section01/script03b.asp, line 9

Moving Forward:

Now that we have Escape Characters and new String( ), everything else we want to do in ASP JavaScript becomes a lot easier.

You ain't seen nothing yet. We can mix and match JavaScript functions and methods with VBScript functions and methods. VBScript and JavaScript functions can pass arguments back and forth. They work wonderfully together. That will be the subject of lesson 04.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值