-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathajaxcall.js
126 lines (114 loc) · 3.07 KB
/
ajaxcall.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
Basic Ajax JavaScript functions used by AjaxPage.
Written by John Dickinson based on ideas from
Apple Developer Connection and DivMod Nevow.
Some changes made by Christoph Zwerschke.
*/
var request_url = document.location.toString();
if (request_url.indexOf('?') >= 0) {
request_url = request_url.substr(0, request_url.indexOf('?'));
}
request_url += "?_action_=ajax"
var request_id = 0;
function getRequester() {
if (window.XMLHttpRequest) {
try {
req = new XMLHttpRequest();
} catch(e) {
req = false;
}
} else if(window.ActiveXObject) { // IE specific
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
req = false;
}
}
}
return req
}
function openConnection(req, url)
{
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
if (req.status == 200) {
try {
eval(req.responseText);
req.abort();
delete req;
} catch (e) {
; // ignore errors
}
}
}
};
req.open("GET", url, true);
req.send(null);
}
}
// Generic Ajax call:
function ajax_call(pre_action, call) {
if (pre_action) {
eval(pre_action);
}
var args = '&_req_=' + ++request_id;
for (i=2; i<arguments.length; i++) {
args += '&_=' + encodeURIComponent(arguments[i])
}
req = getRequester();
if (req) {
openConnection(req, request_url + "Call&_call_=" + call + args);
}
}
// Ajax call specific to forms:
function ajax_call_form(call, form, dest, val) {
if (dest) {
ajax_setTag(dest, val);
}
var values = Array();
for (i=0; i<form.elements.length; i++) {
var e = form.elements[i];
name = e.name;
if (!(((e.type == 'checkbox') || (e.type == 'radio')) && (!e.checked))) {
values[i] = [name, e.value];
}
}
var args = '&_req_=' + ++request_id;
for (i=0; i<values.length; i++) {
args += '&_=' + encodeURIComponent(values[i])
}
req = getRequester();
if (req) {
openConnection(req, request_url + "Call&_call_=" + call + args);
}
}
// Some Ajax helper functions:
function ajax_setTag(which, val) {
var e = document.getElementById(which);
e.innerHTML = val;
}
function ajax_setClass(which, val) {
var e = document.getElementById(which);
e.className = val;
}
function ajax_setID(which, val) {
var e = document.getElementById(which);
e.id = val;
}
function ajax_setValue(which, val) {
var e = document.getElementById(which);
e.value = val;
}
function ajax_setReadonly(which, val) {
var e = document.getElementById(which);
if (val) {
e.setAttribute('readonly', 'readonly');
}
else {
e.removeAttribute('readonly');
}
}