gtajax logo

GTAjax / 通用Ajax ( LastUpdated: )

Author: Wade Lau, wadelau@ufqi.com, wadelau@hotmail.com

Summary / 概述
GTAjax is Generally-Targeted Ajax. Ajax stands for Asynchronous JavaScript And XML.
GTAjax is a kind of Ajax, and beyond the Ajax.
Maybe you have see and have used it in some coding experience. And also, you have not found any inefficent, actually, common Ajax has its own limits in many ways, It depends on the environment heavily.
Mostly, you code one Ajax which can not run in another place with differences in Browser,Operating System,Bankend programming components,etc...
In order to comply with the differences, people creates many kinds of Ajax, amd so do I, luckily enough, I found one generally-targeted way to comply with all the differences... , I call it GTAjax. Another reason for this name is GTAjax also refers to GridTask Ajax .

GTAjax 就是通用(Generally-Targeted)Ajax的意思。是Ajax的一种. Ajax是异步Javascript和XML应用的合称。 可能你已经见到和使用过Ajax的应用,并且还没有发现它有什么不足。实际上,Ajax本身有很多限制,它对环境的依赖很强。
大多数情况是你在一处写的Ajax无法在别的地方使用,它对浏览器,操作系统和后台程序都有不同的限制和要求。
人们为了完成各样环境下的Ajax应用,编写不了不同的Ajax版本,我也一样,幸运的是我找到一个可以通行应用于不同环境下的Ajax,我叫它GTAjax。 取这个名字的另外一个原因是,GTAjax 也代表 GridTask Ajax 的意思。


Feathers / 特征
GTAjax的特征表现在:
1. 具有Ajax的功用,可替代Ajax的工作
2. 其通用性表现在:
    a. 与操作系统无关,这应是javascript的本性
    b. 与浏览器无关,目前我们已经在MSIE,Mozilla firefox,Opera上测试通过
    c. 与Web服务器无关, 不依赖于Web服务器的特质去完成任务,可在流行的Apache,IIS等Web server运营的站点使用
    d. 与后台脚本应用程序无关, 也即针对后台服务器脚本asp,php,jsp,perl等无须做改动
3. 对Ajax的硬伤--文件上传作了单独的优化处理
4. 代码精简,仅有7k左右 , (带表单验证有12K,多实例同时排队机制20+K)
5. 使用简便,可调用操作,设置不同运行参数
6. 无需设置服务器端,这一点听起来很荒谬
7. 表单验证(v1.58+)
8. 多实例同时运行(v3.10+)
9. 改进的表单验证,通过使用 form 内 input 字段的 accept 属性设置,可以实现媲美 html5 方式的表单输入验证(v5.2+) (问题是都 html5 了,这个功能不是鸡肋了吗? —— 等地球上的浏览器都支持 html5 像现在的浏览器都支持 javascript 之后,新增部分会被移除.)
GTAjax's feathers include:
1. do what Ajax can do
2. its general feather as following:
     a. no mater what OS , inherits from javascript
     b. no matter what Browser, it has been tested pass in MSIE, firefox, Opera
     c. no matter what web server, it can used in Apache, IIS, etc
     d. no matter what backend programming language, it can be used with jsp, asp, php, perl,etc
3. for Ajax's shortcoming--file upload, it has been optimised to handle files upload correctly
4. small size, only about 7k , (with form validator up to 12k, with concurrent instances more than 20k)
5. easy to use, with parameters to set for specified running
6. no action needed with server, which sounds unreasonable
7. form validator (v1.58+)
8. multiple instances running in queue (v3.10+)
9. enhanced form input validating functions, same as client-side validators in html5. this needs help from the accept property of input field in form of current html (v5.2+)


Usage / 用法
在您的网页代码中加入:[place the following code in your page]
....
<script type="text/javascript" src="GTAjax_.js"></script>
.... 

然后写出自己的业务处理函数[then, write you own functions]:
v3.10+ 面向对象的调用实例:

写出自己的处理函数 <script type="text/javascript"> function includeFile(strUrl,strId,isCache) { var gtajax = new GTAjax(); if(!isCache) { gtajax.setGTAjax('iscache',false); } gtajax.setGTAjax('targetarea',strId); var sTmp = gtajax.getGTAjax(strUrl); } </script>
v5.2+ 之后,不再支持 setGTAjax / getGTAjax , 统一为 set / get . 然后页面中调用: <div id="div-1"> <script type="text/javascript"> includeFile('abc.jsp','div-1',false);</script> </div>

....

function subMyForm(sFrm)
{
// before submiting...
setGTAjax('returnname','Back'); // do settings 设置环境变量
var getTmpStr = GTAjax(sFrm); // main action 调用主函数处理并取结果
// after submited...
}

v3.10+ 之后,使用面向对象的设置,需在调用前通过 new 操作符生成实例.
GTAjax不但能递交表单,也可处理url请求 [also,GTAjax could handle url request]
function handleMyUrl(sUrl)
{
// before go to next url....
setGTAjax('targetarea','newdiv');// 设置将从服务器端取到的内容显示到新区域
//setGTAjax('returndataonly',true);
setGTAjax('iscache',false); // 设置是否处理缓存,更多设置见附后参考[with more settings in the following part]
var getTmpStr = GTAjax(sUrl); // 主处理函数
// after get remote material...
}  

.... 

接着将这些动作绑定到对象去[then, bind the function to your target]:
....

<span id="formArea">(请将form放到一个对象中去,place form in a named object)
<br/>
<form name="myform" id="myform" action="thisform.do.php" method="post" >
<input type="text" id="userName" name="userName" value="Wade Lau"/><br/>
....
<input type="submit" value="Submit" id="btn1" name="btn1" onclick="javascript:subMyForm(this.form.name);"/>(这里的参数是表单名,here give function the form's name)<br/>
</form>
</span>  

... 

ok,done.现在,开始吧[now, it is time to go] ....


Reference and Download / 参考和下载
@ GTAjax运行时配置参考[settings references]:
....
setGTAjax('subbtnval','Submit'); // 表单递交按钮显示的字符,默认是Submit
setGTAjax('maxuploadfiletime',3*60*1000); // 上传文件等待回应的最大时间,默认是3分钟
setGTAjax('targetarea','areaname'); // 递交请求获取数据后写在当前页面的何处,默认是递交元素所在的区域
setGTAjax('returnname','Return'); // 表单递交后,返回链接的显示字符,默认是Return
setGTAjax('returndataonly',false); // 是否仅仅取得返回数据(而不向目标区域写入),默认是false
setGTAjax('iscache',true); // 是否缓存从服务器取得的内容,默认是true
setGTAjax('forceframe',false); // 是否强制使用iframe技术,即便没有fileupload行为也不使用xmlhttp,在一些xmlhttp受限的地方可使用iframe代替
setGTAjax ('processbar',true); // 是否显示处理中提示条, 默认是显示 (1.89+, idea from ayg at founder dot com)
setGTAjax ('backlink',true); // 是否在返回内容中追加返回链接,默认为是(1.89+ )
setGTAjax('forceframetag','fftag'); //告知服务器端,此次请求是普通请求,非xmlhttp,服务器端可据此fftag=1来判断(1.91+)
setGTAjax('chkform','userMail:email+:user mail should be an valid email address'); // 设定表单递交验证项(详细见下表)


v3.0+的属性设置, 在上述的名称前加一个实例对象:
gtajaxinstance.setGTAjax(name, value) ;
或者
gtajaxinstance.set(name, value);
取值的地方也增加了:
gtajaxinstance.get( object ) ;


v4.0新增属性设置:
gtajaxinstance.set('nobacktag',''); //--- 设定如果服务器端返回的内容中含有此字串则不在内容后面附加"返回"的链接
gtajaxinstance.set('nocopy',false); //--- 设定是否禁止加载页面的内容被复制


v5.2新增属性和不兼容设置:
1.表单验证的写法:
<input type="text" name="email_address" id="email_address" accept="email+::Please input correct Email" />
与之前相比:
1.1 表单验证的设置,可以不再通过 gtajaxinstance.set('chkform','strFormFieldName:formValidateType: errorMessage') 的方式设置
1.2 设置项,不再使用 : ,而是使用2个 : ,也即 :: 来分割设置项内容
....

GTAjax的表单验证(v5.10- 适用, v5.2+ 请参照上面的说明),其配置为:
setGTAjax('chkform','userMail:email+:user mail should be an valid email address');
其中'chkform' : tell GTAjax to validate form data.
'userMail:	email+:		user mail should be an valid email address'
'strFormFieldName:formValidateType:	errorMessage'

Configuration seperation tag ':' --> '::'  since v5.2, 201107

GTAjax Form Validation Descriptors // GTAjax 表单验证关键字描述
formValidateTypeExplainations[partly from http://www.javascript-coder.com ]
required
req
The field should not be empty // 不能为空
maxlen=???
maxlength=???
checks the length entered data to the maximum. For example, if the maximum size permitted is 25, give the validation descriptor as "maxlen=25" // 长度检测,至多允可长度, 如欲限定在25字符内,则使用表达式, "maxlen=25", 在GTAjax中的完整表达为
setGTAjax('chkform','strField:maxlen=25:this field max length is 25.');
minlen=???
minlength=???
checks the length of the entered string to the required minimum. example "minlen=5" //最小限定为
alphanumeric
alnum
Check the data if it contains any other characters other than alphabetic or numeric characters //仅为字母和数字(不含符号等)
num
numeric
Check numeric data // 仅为数字
alpha
alphabetic
Check alphabetic data. // 仅为字母
email The field is an email field and verify the validity of the data. // 限为合法email地址
lt=???
lessthan=???
Verify the data to be less than the value passed. Valid only for numeric fields.
example: if the value should be less than 1000 give validation description as "lt=1000" // 其值不大于
gt=???
greaterthan=???
Verify the data to be greater than the value passed. Valid only for numeric fields.
example: if the value should be greater than 10 give validation description as "gt=10" // 其值不小于
regexp=??? Check with a regular expression the value should match the regular expression.
example: "regexp=^[A-Za-z]{1,20}$" allow up to 20 alphabetic characters. // 其值符合给定的正则表达式
dontselect=??

 Deprecated

This validation descriptor is valid only for select input items (lists) Normally, the select list boxes will have one item saying 'Select One' or some thing like that. The user should select an option other than this option. If the index of this option is 0, the validation description should be "dontselect=0" // 这个是 http://www.javascript-coder.com 的标配, 其本身局限大,功能小,舍弃
notvalue=???

newlyadded

Verify the data to be any data other than the specified value // 是上面一个的的升级和扩容,是一个反向概括,如不能让其值为空"notvalue=" , 不能选择第一个选项"notvalue=1",用户名一项的值不能等于初始值"notvalue=用户名"...
(anyofabove)+

newlyadded

the "+" means req, or required, not empty when added up to any type of validating above, e.g. "email+" which means the expected value is an email address, and the field also is required, not empty. // 在写表单验证时参考了很多目前流行的,这个就是精简的结果,比如其中一项,即要符合email地址规范,又不能为空,则 "email+" , 其中一项只能是数字,且不能留空,则 "num+",...


< 下载/Download
2020.07.04, GTAjax/通用Ajax-5.7 , @GitHub: wadelau/GTAjax
2011.07.20, GTAjax/通用Ajax-5.2
2011.02.05, GTAjax/通用Ajax-5.10
2009.06.07, GTAjax/通用Ajax-4.40
2008.05.19, GTAjax/通用Ajax-4.33
2008.04.10, GTAjax/通用Ajax-4.26
2008.02.24, GTAjax/通用Ajax-4.25
2007.10.04, GTAjax/通用Ajax-4.01
2007.07.22, GTAjax/通用Ajax-3.10 (允许同时运行多个实例)
2007.05.16, GTAjax/通用Ajax-2.01
2006.10.12, GTAjax/通用Ajax-1.91
2006.09.22, GTAjax/通用Ajax-1.89
2006.07.25, GTAjax/通用Ajax-1.86
2006.07.07, GTAjax/通用Ajax-1.72
2006.05.23, GTAjax后端脚本之aspx
2006.05.23, GTAjax后端脚本之php
2006.03.22, GTAjax/通用Ajax-1.69
2006.03.15, GTAjax/通用Ajax-1.58, with form validator.
2006.03.12, GTAjax/通用Ajax-1.36
2006.02.17, GTAjax/通用Ajax-1.28


Thanks and Declarations / 致谢与声明
I wanna say THANKS :
to my home, they do more and spare me to have time code these. 感谢我的家人,他们更多的操劳才使我有时间写这些.
to Google, without its motivation, I have no idea about Ajax, and without it, I hardly complete this code.感谢Google,因其而起而又得其力.
to dong_li98@hotmail.com, we do together an exciting action by submitting a form twice with javascript, which contributes mainly to this script. 感谢dong_li98@hotmail.com,我们一起制造了一个表单递交两次的尝试,这个思想对本文影响很大.
to AjaxAnywhere[JSP based Ajax], to Prototype[javascript extend library], to W3C, to RFC, to DOM, to lots of people who works on js and share their scripts with us ... 还要感谢AjaxAnywhere[jsp 环境中的Ajax],Prototype[javascript的扩展库], W3C, RFC, DOM 和其他许多在网上共享他们的js脚本的人...

NOTICE:
DO NOT USE THIS COMMERICALLY WITHOUT AUTHOR'S PAPER AUTHORIZATION.仅供个人免费使用,请勿复制用于商业获利.
if needed, please contact with us at wadelau@ufqi.com 如有必要请联络 wadelau@ufqi.com 获取授权.



Instance/ 实例
This is a test for GTAjax:

您的电邮[Your E-Mail]:

评论[Comment]:

你的图片[Your Photo]:



Comments and History / 评论与历史
( GTAjax的FAQ支持应答系统:  http://ufqi.com/qa/gtajax.html  since 2008.03.13 )


H Return/返回