关闭

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

636人阅读 评论(0) 收藏 举报
 

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.

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4033次
    • 积分:45
    • 等级:
    • 排名:千里之外
    • 原创:0篇
    • 转载:4篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档