js ajax library,AJAX Only Javascript Library

Here' a small chunk of a JavaScript PHP wrapper I wrote a long time ago when I virtually knew nothing about JavaScript... it barely contains any AJAX methods, just wrapper functions that communicate with a PHP backend in order to allow PHP to do all the work...

In all honesty, to get what it seems you're looking for... just sit down and write yourself an AJAX library with all the common helper functions. It should take you a few hours at the most.

The Javascript:

(function() {

var

PHPJS = window.PHPJS = window.$ = function() {

return new PHPJS.Strings;

};

PHPJS.Strings = PHPJS.prototype = {

InitAJAX: function(Library, ServerString)

{

var ResultCache = document.body;

var FunctionRequest;

try {

FunctionRequest = new XMLHttpRequest();

} catch (e) {

try {

FunctionRequest = new ActiveXObject("Msxml2.XMLHTTP");

} catch (e) {

try {

FunctionRequest = new ActiveXObject("Microsoft.XMLHTTP");

} catch (e) {

throw new Error("The XMLHttpRequest() object is not supported by your browser.")

return false;

}

}

}

FunctionRequest.onreadystatechange = function() {

if (FunctionRequest.readyState == 4 && FunctionRequest.status == 200) {

ResultCache.innerHTML = FunctionRequest.responseText;

return FunctionRequest.responseText;

}

}

switch (Library) {

case 'Arrays' :

FunctionRequest.open("GET", "functions/arrays-lib.php" + ServerString, true);

break;

case 'Math' :

FunctionRequest.open("GET", "functions/math-lib.php" + ServerString, true);

break;

case 'Strings' :

FunctionRequest.open("GET", "functions/strings-lib.php" + ServerString, true);

break;

}

FunctionRequest.send(null);

},

/* String Functions */

addcslashes: function(str, charlist)

{

return this.InitAJAX('Strings','?function=addcslashes&String='+str+'&Charlist='+charlist);

},

addslashes: function(str)

{

return this.InitAJAX('Strings','?function=addslashes&String='+str);

},

bin2hex: function(str)

{

return this.InitAJAX('Strings','?function=bin2hex&String='+str);

},

chop: function(str,charlist)

{

return this.InitAJAX('Strings','?function=chop&String='+str+'&Charlist='+charlist);

},

chr: function(int)

{

return this.InitAJAX('Strings','?function=chr&Int='+int);

},

chunk_split: function(str, chunklen, end)

{

return this.InitAJAX('Strings','?function=chunk_split&String='+str+'&Chunklen='+chunklen+'&End='+end);

},

convert_cyr_string: function(str)

{

return this.InitAJAX('Strings','?function=convert_cyr_string');

},

/* more functions... */

}

})();

//PHPJS.bin2hex('afsdfadsafdsafdasfsaf');

The PHP:

switch($_GET['function']) {

case 'addcslashes' :

$charlist = (@$_GET['Charlist']) ? ','.$_GET['Charlist'] : '';

echo addcslashes($_GET['String'], $charlist);

break;

case 'addslashes' :

echo addslashes($_GET['String']);

break;

case 'bin2hex' :

echo bin2hex($_GET['String']);

break;

case 'chop' :

$charlist = (@$_GET['Charlist']) ? ','.$_GET['Charlist'] : '';

echo rtrim($_GET['String'], $charlist);

break;

case 'chr' :

echo chr($_GET['Int']);

break;

case 'chunk_split' :

echo chunk_split($_GET['String'], @$_GET['Chunklen'], @$_GET['End']);

break;

/** ...etc, etc... **/

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值