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

转载 2006年06月24日 13:34:00
 

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.

相关文章推荐

C++ SDL教程翻译 Lesson 03 Event Driven Programming

初二的我又来啦!这是第三篇翻译,嗯~ 末尾还是有附加内容,翻译中所说的源代码下载请去结尾,我给出了地址。 警告:转载此翻译文请注明来自CSDN博客,CSDN博客是此翻译唯一出处。 中文版: ...

JavaScript---对象学习(一)object对象、string对象、javaScript的constructor属性和prototype属性

JavaScript对象学习那么我们就先看看看object对象把object对象学习 查看帮助文档 ,得到了这些信息 Object 对象 提供所有 JScript 对象通用的功能。 obj = n...

JavaScript基础教程03var动态数据类型

var变量,var变量的初步体验,JavaScript 拥有动态类型相信大家可以在短时间内掌握。...

彻底理解原型、原型链、new操作符、prototype、__proto__、constructor等属性的概念

原型和原型链 构造函数 new操作符

Ext JS 6应用程序Build后出现“c is not a constructor return new c(a[0])”的处理

Ext JS 6应用程序Build后出现“c is not a constructor return new c(a[0])”的处理

Class.newInstance()与new、Constructor.newInstance()的区别

在初始化一个类,生成一个实例的时候,newInstance() 和 new 有什么区别?   用newInstance与用new是区别的,区别在于创建对象的方式不一样,前者是使用类加载机制,那么为...

兔子--The constructor AlertDialog.Builder(new View.OnClickListener(){}) is undefined

出错原因:Builder builder=new Builder(this) 该方法里面的this为当前类OnClickListener的实例, 而不是我们想要赋值的MainActivity.thi...

JavaScript初级教程(new)

  • 2008年06月04日 21:38
  • 187KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Asp JavaScript 教程 - Lesson 03 The new String( ) Constructor
举报原因:
原因补充:

(最多只允许输入30个字)