A
detailed description of both Types and Classes is
below.


Problem:
What is the difference between a Type and a Class in
LotusScript?

Solution:
A detailed description of both Types and
Classes is below.

I. TYPES

In LotusScript, data types can be
defined with data members (variables) that
can be manipulated as a
single
unit. Types can be used to build a record structure to store database records
within LotusScript.

A type is defined with the TYPE...END
TYPE statement. Within this statement,
the type members are
declared
(without the DIM statement).

For example, to declare a type named
OrderInfo, with the members ID (a
fixed-length string),
CustomerName (a variable length string) and TotalPrice (a currency value); the
following
statements could be
used:

TYPE OrderInfo
ID AS STRING *
6
CustomerName AS STRING
TotalPrice AS CURRENCY
END TYPE

After
declaring a type, a variable can be declared of this type, just as a

variable could be declared of a built-in
data type. For example, to
declare a variable to contain a single order:

DIM OrderVariable AS
OrderInfo

A fixed array of 10 orders can also be declared:

DIM
OrderArray (10) AS OrderInfo

Each of the ten elements of this array is an
instance of the OrderInfo type.
Each of these instances includes
one of
each type member:

ID
CustomerName
TotalPrice

Members of a type can have any built-in data type to another user-defined type;
however,
a type member
cannot contain an instance of itself.

Use Dot Notation
to refer to a member of a type variable. For example, in the
type defined
above, the
members of the instance OrderVariable can be accessed as
follows:

DIM OrderVariable AS OrderInfo
OrderVariable.ID =
"001"
OrderVariable.CustomerName = "Joe Jones"
OrderVariable.TotalPrice =
35.00

II. CLASSES

Classes are similar to types but classes go one
step further. Like types,
classes can declare aggregates of data
which can be manipulated as a single unit. Classes also allow subprograms to be
declared which manipulate
the class data. The data and the subprograms
together form a class; a single
unit. A programming language
such as
LotusScript which allows programs based on classes to be created is
called
an object-oriented
programming language.

A class is defined similar to
a type. Variables are defined which are
aggregated in the class. The
subprograms
or methods can also be defined that manipulate the class
variables or data
members. Methods can take the
form of subs, functions
or properties.

Once a class is defined, the instances of the class are
created. These
instances are called objects. The
CLASS...END CLASS
statement is used to create a class definition. Classes can
only be defined
at the
module level.

For example, in the following script, a Customer
class is declared with data
members called CustName,
Address and Balance.
This class also includes a subprogram member, called

CheckOverdue.

CLASS Customer
PUBLIC CustName AS STRING
PUBLIC
Address AS STRING
Balance AS CURRENCY

SUB CheckOverdue 'This
subprogram takes no arguments
IF Balance > 0 THEN
PRINT "Overdue
Balance"
END IF
END SUB
END CLASS

Private versus
Public:

Class members can be Public or Private. By default, class data
members are
Private and class subprogram
members (methods) are Public.
Public class members can be referred to outside
of the class definition;
Private
members cannot. Private data members are hidden from subprograms
defined
outside of the class. Data hiding
helps programmers structure
their programs to limit access to member data. In
the above example, the
only
subprograms which can refer to Balance data are the member subprograms

(methods) defined in the class
Customer; for example, the member
subprogram CheckOverdue defined above. The
data members
CustName and
Address are declared Public, so these data members can be referred
to by
subprograms
defined outside of the class Customer.

After defining a
class, the DIM statement may be used to declare variables
which refer to
objects of that class.
When an object reference variable is declared, an
object itself is not created.
The object reference variable
is
initialized to contain the special value NOTHING.

For
example:

DIM X AS Customer
'This statement declares an object
reference variable

X can hold either references to Customer objects or
the value NOTHING. X is
initialized to NOTHING.

Once an object
reference variable has been created, an object can be created in
one of two
ways. The first is
to use the keyword NEW in the declaration statement for
the object reference
variable. Using the keyword
NEW declares an object
reference variable, creates an object and assigns a
reference to the
newly-created
object to the variable.

For example:

DIM X AS NEW
Customer

The second way to create an instance of a class is to use a SET
statement which
includes the NEW keyword
and a variable which has been
previously declared as an object reference
variable for that
class.

For example:

DIM X AS Customer
SET X = NEW
Customer

Once an object has been created, Dot Notation may be used to
refer to the data
members and methods
associated with the object of the
given class; similar to using Dot Notation
with a type. Dot Notation is used
to
refer to an individual member of an object. Dot Notation is also used to

reference member subprograms
(methods) in a class.

For objects
declared outside the class, Dot Notation can only be used to access
data
members that are
Public. For example, if X is a Public object reference
variable of the class
Customer, the Public data member
Balance in the
class Customer is referred to as X.Balance outside the class
definition. If
the data member
Balance was not declared using the PUBLIC keyword, it would
be accessible only
from within the class
definition; not outside of
it.

For example:

CLASS Customer
PUBLIC CustName AS
STRING
PUBLIC Address AS STRING
PUBLIC Balance AS CURRENCY

SUB
CheckOverdue ' This subprogram takes no arguments
IF Balance > 0
THEN
PRINT "Overdue Balance"
END IF
END SUB
END CLASS

DIM X
AS NEW Customer
'This statement declares an object reference variable X of
the class Customer
and also creates an object of
that class and assign X
a reference to the new Customer object.

X.CustName = "Acme
Corp."
'This statement is legal because CustName is Public.

X.Balance
= 35.00
'This statement is legal because Balance is
Public.

X.CheckOverdue
'This statement invokes the member subprogram
CheckOverdue. Since this sub
takes no arguments, no
arguments are passed
when the sub is called. In this instance, the text
"Overdue Balance" would
print since
X.Balance is greater than zero.

Members of the Current
Object within a class subprogram (method) can be
declared by using the
ME
keyword. The ME keyword is not valid outside of a class method
(subprogram).

For example:

CLASS Customer
PUBLIC CustName AS
STRING
PUBLIC Address AS STRING
PUBLIC Balance AS CURRENCY

SUB
CheckOverdue 'This subprogram takes no arguments
IF ME.Balance > 0
THEN
PRINT ME.Balance
ELSE
PRINT "No overdue Balance"
END IF
END
SUB
END CLASS

DIM X AS NEW Customer
'This statement declares an
object reference variable X of the class Customer.
It also creates an object
of that
class and assign X a reference to the new Customer
object

X.CustName = "Acme Corp."
'This statement is legal because
CustName is Public.

X.Balance = 35.00
'This statement is legal because
Balance is Public.

X.CheckOverdue
'This statement invokes the member
subprogram CheckOverdue. Since this sub
takes no arguments, no
arguments
are passed when the sub is called. In this instance, the value of
X.Balance
would print since
X.Balance is greater than zero.

For additional
information about Types and Classes, refer to the LotusScript
Reference
Guide, Chapters 3
and 5, respectively.
posted @ 2012-02-01 10:38 hannover 阅读(8) 评论(0) 编辑

This an implementation of Base64 as described in rfc4648 (The Base16, Base32, and Base64 Data Encodings) for the Lotus Notes environment.

Base64 is an algorithm to encode binary data into a ascii text representation. Common applications are

  • Passing credentials to web servers: HTTP Authentication: Basic and Digest Access Authentication.
  • Transferring binary data in email:For this purpose, the encoding can be run in MIME-mode to produce line breaks.
  • Transferring or storing binary data in other formats, like XML.

This library makes use of NotesStreams and NotesMIMEEntity that came with Notes 6.

Usage

  1. Save and unzip the code to a text file (libBase64.lss)
  2. Use Domino Designer to create a new empty Lotus Script library ‘libBase64′
  3. Import libBase64.lss and save the lib
  4. Now you can include the library (Use "libBase64") and use it:
    Dim b64 As New CBase64()
    Call b64.encode (..)

Samples

 


Encode a string to base64

Dim b64 As New CBase64()

	Print b64.encodeString ("foobar")
	

Encode a binary file to a base64 encoded file

Dim b64 As New CBase64()

	Call b64.encodeFileToFile (fspecInput, fspecOutput)
	

The result file will have line breaks at column 76. If this is not desired, than it can be suppressed. This will be a bit slower however:


	Dim b64 As New CBase64()
	b64.bMimeModeEncoding = False
	Call b64.encodeFileToFile (fspecInput, fspecOutput)
	

Decode a base64 encoded file to a (probably binary) file

Dim b64 As New CBase64()

Call b64.decodeFileToFile (fspecInput, fspecOutput)

	

 



posted @ 2012-01-23 11:24 hannover 阅读(12) 评论(0) 编辑

问题描述:

Domino的用户可以自己通过Web的方式去修改internet密码,更改结束后,旧的密码被放到服务器的缓存中,保留两天来确保管理请求数据库和domino通讯录之间的复制完成。更改密码的请求是由adminP这个任务,在管理请求数据库admin4.nsf中发起“change http password in domino directory”的管理请求从而得以完成的。如果这个默认的缓存时间过长或者过短怎么办?能调整这段缓存时间吗?

解答:

缓存时间可以通过在server的notes.ini里面添加以下参数来修改:

HTTP_Pwd_Change_Cache_Hours

使用的格式如下:

HTTP_Pwd_Change_Cache_Hours=X(X是旧的密码保存在缓存中的小时数目)。

在这段缓存时间内,旧的密码和新的密码都是有效的。

此外,如果服务器有设置过安全性策略文档,在“口令管理”标签下面,“允许用户通过 HTTP 改变 Internet 口令”这个域值应该是“是”。

posted @ 2012-01-23 11:22 hannover 阅读(12) 评论(0) 编辑

上个月,由于邮件系统转移到DOMINO平台上,发生了一件非常奇怪的事情.

整个情况:内网邮件接发正常,可以接到外网邮件,但是发出的外网邮件,提示发送成功,但是对方却收取不到.

分析:邮件系统是由2台server组成的.

domino的邮件收取是由代理完成,即server1完成内部邮件的发送收取,外部邮件的发送.

按上面的情况,这3点正常就证明server1是不存在问题的.

向外发,由server2发向server1,server1转发出去,DOMINO进行邮件发送时候,所有有问题的邮件可以用domino administrator 消息处理检查mailbox,找到发不出的邮件,打开,可以看见正式错误提示

另外,domino邮件系统可以提供邮件追踪,可以追踪到邮件状态.

当我们在mailbox找到邮件的时候可以确定:邮件没有发出.

当时唯一通过google收集的有效信息就是,DNS出现错误了.

检查server的IP配置,可以确定没有问题,可是问题再那里呢?

再google提示到双网卡的问题,由于DOMINO是不识别双网卡的,双网卡会导致识别不了DNS.可是,我们只对一块网卡设置了IP,另一块的禁用的.

事实证明DOMINO就是这么笨蛋,只要有二块网卡DNS就是不认.

解决的办法很简单.

在domino目录的notes,ini文件中间增加一句话:DNSServer=DNS IP.例如是深圳的用户:DNSServer=202.96.134.133,这样就可以确信DOMINO可以识别正确的DNS了

一些邮件问题解决步骤:

●邮件不能发送出去?
【对策】
检查[配置]—[服务器/配置]—[编辑]:
a.发件人地址
b.任务
c.目的服务器
d.网络连接
e.路由时间安排
f.连接文档
g.邮件大小
h.优先级与待发邮件数
i.邮递限制j.宿主邮件服务器

●收到退信?
【对策】
查看:消息处理、邮件、public邮箱(mail.box)、Console command:show server
处理:删除或释放死锁邮件

●发出,无退信,但对方无法及时收到?
【对策】
检查:1)用户惯用选项—端口—跟踪—跟踪,选择跟踪条件,发跟踪邮件,返回跟踪报告。
      2)路由时间安排

[color=blue]★邮件诊断操作步骤[/color]

☆配置邮件跟踪:
配置、服务器、配置、填加配置(或编辑现有的服务器配置文文件)
基本:使用这些设置作为所有服务器的缺省设置(是)
或  添入具体的群组或服务器名称
路由器/SMTP、消息跟踪:
消息跟踪:启用
记录消息主题:是
消息跟踪集合时间间隔:15
允许跟踪消息:加入服务器名
允许跟踪主题:
保存退出

☆启动、停止邮件跟踪任务
配置邮件跟踪后,在服务器启动时,会自动创建“邮件跟踪储存”数据库:
\lotus\domino\data\mtdata\mtstore.nsf
服务器启动时,自动启动服务器上的邮件跟踪。
手动:
Load  mtc
Tell  mtc  quit
Tell  mtc  process

☆发送跟踪邮件:
消息处理、邮件、工具、消息处理、发送邮件跟踪
收件人:
主题:
发送跟踪报告来自:
发送、完成

☆查看邮递报告
消息处理、邮件、邮件路由事件
打开相关文档,查看邮递报告或打开邮箱,会收到邮递报告

☆跟踪中心:
消息处理、跟踪中心
新建跟踪请求
从;到;发送;开始;主题等
确定
选择一个邮件
跟踪选定的信息
查看跟踪结果

[color=blue]★邮箱方面操作点滴[/color]

◆优化邮件
多个邮箱:
设置多个(3--5)MAIL.BOX,同时处理邮件
减少争夺
增加可靠性
提高传送速度
配置、服务器、配置、打开服务器配置文文件、路由器/SMTP
基本:邮箱数(N)
RES  S

◆共享邮件:
邮件:
信头:发送给所有用户
信体:发送一份到共享邮箱中
配置、服务器、配置、打开服务器配置文文件
在NOTES.INI中加入SHARED_MAIL=1
RES  S

◆邮件限额:限制NOTES用户的邮箱和邮件的大小
1、邮件文件
文件、MAIL活页夹、选一个用户文件、工具、数据库、限额、限制限额及警告值
在其它消息中可疑查看限额
在建立用户时,也可以进行限额设定操作
2、每个邮件的限额
配置、服务器、配置、打开配置文文件、路由器/SMTP、限制和控件、限制
最大消息长度

◆邮件内容格式:
用户个人文文件:邮件、外出邮件的惯用格式
场所文档:编辑场所、邮件、寻址发送到internet的邮件的消息格式

◆设置用户执行邮件代理
编辑服务器文文件、安全性、代理限制、在“运行受限制的LotusScript/Java代理”中填加用户

posted @ 2012-01-23 11:21 hannover 阅读(25) 评论(0) 编辑

The following table summarizes the known maximum limits of various Notes and Domino features.

Item

Maximum limit

Database size

The maximum OS file size limit -- (up to 64GB)

Text field size

32KB (storage); 32KB displayed in a view's column

Rich text field size

Limited only by available disk space up to 1GB

Response levels in a hierarchical view; number of documents per level

31 levels; 300,000 documents

Characters in names

Database Title: 96 bytes

Filenames: On Windows® and UNIX®platforms minimum of 255 and/or OS limits; on local Macintosh workstation 31

Field names: 32

View names: 64

Form names: 32

Agent names: 32

Fields in a database

~ 3000 (limited to ~ 64K total length for all field names). You can enable the database property "Allow more fields in database" to get up to 22,893 uniquely-named fields in the database.

Columns in a table

64

Rows in a table

255

Views in a database

No limit; however, as the number of views increases, the length of time to display other views also increases

Forms in a database

Limited only by database size.

Columns in a view

289 ten-character columns; dependent upon # or characters per column

Documents imported into a view

Documents totaling at least 350K

Cascading views in a database

200

Margin size (in inches)

46

Page cropping size (in inches)

46

Point size to select or print

250

Documents in a view

Maximum of 130MB for a view index

Documents that can be exported to tabular text

Limited only by available disk space

Entries in an Access Control List (ACL)

~950 names (ACL size is limited to 32767 bytes)

Roles in an Access Control List

75 Roles

ID password length

63 characters

Authorized users on a multiple password ID

8 users

Outline entries in an outline

~21,000 entries

posted @ 2012-01-23 11:20 hannover 阅读(11) 评论(0) 编辑
 举例为帐号申请单,在开单的时输入一个帐号,系统判断该帐号是否存在于系统中。

1.在表单的JS Header中写判断的javascript函数:

var request;
function checkloginname(){
request = new ActiveXObject("Msxml2.XMLHTTP")
if (!request){
  request=new ActiveXObject("Microsoft.XMLHTTP");}
  request.onreadystatechange=aftercheckloginname;

  //这里假设数据库路径为mis/accounts.nsf,且表单中输入帐号的域是account,将这个域的值传递到代理中
  url="/mis/accounts.nsf/checkRepeatId?openagent&Id="+document.forms[0].account.value;
  request.open("post",url,true);
  request.send(null);
}
function aftercheckloginname(){
if (request.readystate==4){
  if (request.status==200){
   if (request.responseText.indexOf("1")>-1){
    alert (" 对不起,该帐号已经被使用!");
    document.forms[0].account.value="";
    document.forms[0].account.focus();
   }
  }
}
}

2.新建一个checkRepeatId的代理:

Sub Initialize
Dim ss As New NotesSession
Dim doc,docx As NotesDocument
Dim view As NotesView
Dim db As NotesDatabase

Set doc=ss.DocumentContext
Set db=ss.CurrentDatabase
Set view=db.GetView("checkid")     '这个试图即为帐号的试图,试图第一列为帐号
macro=|@RightBack(Query_String_Decoded;"=")|    '这句是获取从URL传过来的参数

id=Evaluate(macro,doc)

Set docx=view.GetDocumentByKey(id(0),True)
Print "Content-type: text/xml"
If Not docx Is Nothing Then
  Print "1"
Else
  Print "0"
End If
End Sub

3.在表单中输入帐号的域,这里举例为account,在域的onchange或者onblur事件中调用javascript方法checkloginname()

posted @ 2012-01-23 11:18 hannover 阅读(14) 评论(0) 编辑
一、只读链接
thisDb:=@ReplaceSubstring(@ReplaceSubstring(@Subset(@DbName;-1);" ";"+");"\\";"/");
@If(@Attachments!=0;"[<a href=\"/"+thisDb+"/0/"+@Text(@DocumentUniqueID)+"/$FILE/"+@AttachmentNames+"\" target=\"_blank\">"+@AttachmentNames+"</a>]";"")

二、可删除链接
thisDb:=@ReplaceSubstring(@ReplaceSubstring(@Subset(@DbName;-1);" ";"+");"\\";"/");
@If(@Attachments!=0;"[<INPUT TYPE=checkbox NAME=\"%%Detach.1\" VALUE=\""+@AttachmentNames+"\"><a href=\"/"+thisDb+"/0/"+@Text(@DocumentUniqueID)+"/$FILE/"+@AttachmentNames+"\" target=\"_blank\">"+@AttachmentNames+"</a>]";"")


但是,当附件的名称中包含“#”、“&”等特殊符号时(例如jeep#beijing.jpg),上面写的链接在特殊符号处就会被截断,从而导致附件不能正常打开。要解决这个问题,需要在写链接的时候把这些特殊符号进行转换,具体方法如下:
thisDb:=@ReplaceSubstring(@ReplaceSubstring(@Subset(@DbName;-1);" ";"+");"\\";"/");
aa:=@URLEncode("domino";@AttachmentNames);
@If(@Attachments!=0;"[<a href=\"/"+thisDb+"/0/"+@Text(@DocumentUniqueID)+"/$FILE/"+aa+"\" target=\"_blank\">"+@AttachmentNames+"</a>]";"")
posted @ 2012-01-23 11:17 hannover 阅读(18) 评论(0) 编辑
Public Function urlDecode(s As String) As String
	If Len(s) = 0 Then Exit Function
	Dim i As Integer
	Dim tmp As String
	Dim c As String
	For i = 1 To Len(s)
		c = Mid$(s, i, 1)
		If c = "+" Then c = " "
		If c = "%" Then
			c = Chr$("&H" + Mid$(s, i + 1, 2))
			i = i + 2
		End If
		tmp = tmp + c
	Next i
	urlDecode = tmp
End Function

Public Function urlEncode(s As String) As String
	If Len(s) = 0 Then Exit Function
	
	Dim tmp As String
	Dim c As String
	Dim i As Integer
	
	For i = 1 To Len(s)
		c = Mid(s, i, 1)
		If (Asc(c) >= 65 And Asc(c) <= 90) _
		Or (Asc(c) >= 97 And Asc(c) <= 122) _
		Or (Asc(c) >= 48 And Asc(c) <= 58) _
		Or Asc(c) = 38 _
		Or (Asc(c) >= 45 And Asc(c) <= 47) _
		Or Asc(c) = 58 Or Asc(c) = 61 _
		Or Asc(c) = 63 Or Asc(c) = 126 Then
			tmp = tmp + c
		Else
			tmp = tmp + "%" + Hex(Asc(c))
		End If
	Next i
	urlEncode = tmp
End Function
posted @ 2012-01-23 10:40 hannover 阅读(10) 评论(0) 编辑
在写b/s应用的时候,经常有检查域有效性的需求。我们一般的做法是用写javascript函数检查域的有效性,通过后再提交,不通过则提示出错。建一个最简单的表单,包含一个用javaapplet形式显示的 rtf域,名为MYRTF。

在写b/s应用的时候,经常有检查域有效性的需求。我们一般的做法是用写javascript函数检查域的有效性,通过后再提交,不通过则提示出错。
现在有这样一个需求,就是在b/s上检查notes自带的rtf javaapplet编辑器内容。也就是说我们得想办法用js访问到这个notes编辑器。

建一个最简单的表单,包含一个用javaapplet形式显示的 rtf域,名为MYRTF。在web上预览,查看源文件得到如下html代码:

<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=GB2312">

<SCRIPT LANGUAGE="JavaScript">
<!--
document._domino_target = "_self";
function _doClick(v, o, t, h) {
var form = document._rtftest;
if (form.onsubmit) {
var retVal = form.onsubmit();
if (typeof retVal == "boolean" && retVal == false)
return false;
}
var target = document._domino_target;
if (o.href != null) {
if (o.target != null)
target = o.target;
} else {
if (t != null)
target = t;
}
form.target = target;
form.__Click.value = v;
if (h != null)
form.action += h;
form.submit();
return false;
}

function _getEditAppletData(){
var form = document._rtftest;
for(i=0;i<form.elements.length;i++) {
if(form.elements[i].editorApplet != null) {
form.elements[i].value = form.elements[i].editorApplet.getText("text//html");
}
}
return true;
}
// -->
</SCRIPT>
</HEAD>
<BODY TEXT="000000" BGCOLOR="FFFFFF">

<FORM onSubmit="_getEditAppletData(); return true;" METHOD=post ACTION="/application/test/William/test.nsf/rtftest?OpenForm&Seq=1" NAME="_rtftest">
<INPUT TYPE=hidden NAME="__Click" VALUE="0">
<APPLET NAME="lnaMYRTF" CODE="Lotus.notes.apps.editorapplet.EditorApplet.class" CODEBASE="/domjava" ARCHIVE="editor.cab" ALT="Editor" TITLE="rtftt" WIDTH="100%" HEIGHT="100%" MAYSCRIPT>
<PARAM NAME="cabbase" VALUE="editor.cab">
<PARAM NAME="locale" VALUE="zh-cn">
</APPLET>

<INPUT TYPE=hidden NAME="MYRTF">
<SCRIPT LANGUAGE="JavaScript">
<!--
document._rtftest.MYRTF.editorApplet = document.lnaMYRTF;
// -->
</SCRIPT>
</FORM>
</BODY>
</HTML>

可以看到 domino为MYRTF域产生了两个元素。一个名为lnaMYRTF的APPLET和一个名为MYRTF的hidden input。并且通过js语句将这两个元素关联起来(document._rtftest.MYRTF.editorApplet = document.lnaMYRTF)。

以上这些对我们来说只是表面现象,我们关心的问题在js函数_getEditAppletData()中得到解答。我们只要使用applet的.getText("text//html")方法就可以得到applet的内容了。但是,用这种方法的得到的是rtf域中带格式的html内容(相当于innerHTML),如果我们想得到不带格式的文本内容可以用.getText("")。
以下就是我用来判断rtf编辑器是否为空的js语句:Empty=(obj.editorApplet.getText("").replace( /^s/, "" ).replace( /s$/, "" )=="")
至此问题解决。

回过头再看html源文件,我们可以看到domino对表单提交的处理。表单在提交时调用 _getEditAppletData()函数寻找所有editorApplet关联的字段,然后将对应applet中的带格式的html内容赋值给对应字段,然后继续提交工作。

posted @ 2012-01-21 21:10 hannover 阅读(12) 评论(0) 编辑
JavaScript中有三个可以对字符串编码的函数,分别是:
escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent

下面简单介绍一下它们的区别


1 escape()函数


定义和用法
escape() 函数可对字符串进行编码,这样就可以在所有的计算机上读取该字符串。


语法
escape(string)


参数 描述
string 必需。要被转义或编码的字符串。


返回值
已编码的 string 的副本。其中某些字符被替换成了十六进制的转义序列。


说明
该方法不会对 ASCII 字母和数字进行编码,也不会对下面这些 ASCII 标点符号进行编码: - _ . ! ~ * ' ( )
。其他所有的字符都会被转义序列替换。



2 encodeURI()函数
定义和用法
encodeURI() 函数可把字符串作为 URI 进行编码。


语法
encodeURI(URIstring)


参数 描述
URIstring 必需。一个字符串,含有 URI 或其他要编码的文本。


返回值
URIstring 的副本,其中的某些字符将被十六进制的转义序列进行替换。


说明
该方法不会对 ASCII 字母和数字进行编码,也不会对这些 ASCII 标点符号进行编码: - _ . ! ~ * ' ( ) 。


该方法的目的是对 URI 进行完整的编码,因此对以下在 URI 中具有特殊含义的 ASCII 标点符号,encodeURI()
函数是不会进行转义的:;/?:@&=+$,#


 


3 encodeURIComponent() 函数


定义和用法
encodeURIComponent() 函数可把字符串作为 URI 组件进行编码。


语法
encodeURIComponent(URIstring)


参数 描述
URIstring 必需。一个字符串,含有 URI 组件或其他要编码的文本。


返回值
URIstring 的副本,其中的某些字符将被十六进制的转义序列进行替换。


说明
该方法不会对 ASCII 字母和数字进行编码,也不会对这些 ASCII 标点符号进行编码: - _ . ! ~ * ' ( ) 。


其他字符(比如 :;/?:@&=+$,# 这些用于分隔 URI 组件的标点符号),都是由一个或多个十六进制的转义序列替换的。


提示和注释
提示:请注意 encodeURIComponent() 函数 与 encodeURI() 函数的区别之处,前者假定它的参数是 URI
的一部分(比如协议、主机名、路径或查询字符串)。因此 encodeURIComponent() 函数将转义用于分隔 URI 各个部分的标点符号。


 


4 总结:


通过对三个函数的分析,我们可以知道:escape()除了 ASCII
字母、数字和特定的符号外,对传进来的字符串全部进行转义编码,因此如果想对URL编码,最好不要使用此方法。而encodeURI()
用于编码整个URI,因为URI中的合法字符都不会被编码转换。encodeURIComponent方法在编码单个URIComponent(指请求参数)应当是最常用的,它可以讲参数中的中文、特殊字符进行转义,而不会影响整个URL。


 


5 示例:


1 escape()


<script type="text/javascript">


document.write(escape("http://www.w3school.com.cn") + "<br
/>")


document.write(escape("?!=()#%&"))


</script>输出:


< type="text/javascript">


http%3A//www.w3school.com.cn


%3F%21%3D%28%29%23%25%262 encodeURI()


<script type="text/javascript">


document.write(encodeURI("http://www.w3school.com.cn")+ "<br
/>")


document.write(encodeURI("http://www.w3school.com.cn/My first/"))


document.write(encodeURI(",/?:@&=+$#"))


</script>输出:


http://www.w3school.com.cn


http://www.w3school.com.cn/My%20first/


,/?:@&=+$#


对整个URL进行编码,而URL的特定标识符不会被转码。


3 encodeURIComponent()


 


例1:


<script type="text/javascript">


document.write(encodeURIComponent("http://www.w3school.com.cn"))


document.write("<br />")


document.write(encodeURIComponent("http://www.w3school.com.cn/p
1/"))


document.write("<br />")


document.write(encodeURIComponent(",/?:@&=+$#"))


</script>


输出:


http%3A%2F%2Fwww.w3school.com.cn

http%3A%2F%2Fwww.w3school.com.cn%2Fp%201%2F

%2C%2F%3F%3A%40%26%3D%2B%24%23
例2:<script
language="javascript">document.write('<a
href="http://passport.baidu.com/?logout&aid=7&u='+encodeURIComponent("http://cang.baidu.com/bruce42")+'">退出</a>');


</script>


对URL中的参数进行编码,因为参数也是一个URL,如果不编码会影响整个URL的跳转。

posted @ 2012-01-21 09:52 hannover 阅读(13) 评论(0) 编辑