ajax.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. [Discuz!] (C)2001-2099 Comsenz Inc.
  3. This is NOT a freeware, use is subject to license terms
  4. $Id: ajax.js 34259 2013-11-26 07:37:25Z nemohou $
  5. */
  6. function _ajaxget(url, showid, waitid, loading, display, recall) {
  7. waitid = typeof waitid == 'undefined' || waitid === null ? showid : waitid;
  8. var x = new Ajax();
  9. x.setLoading(loading);
  10. x.setWaitId(waitid);
  11. x.display = typeof display == 'undefined' || display == null ? '' : display;
  12. x.showId = $(showid);
  13. if(url.substr(strlen(url) - 1) == '#') {
  14. url = url.substr(0, strlen(url) - 1);
  15. x.autogoto = 1;
  16. }
  17. var url = url + '&inajax=1&ajaxtarget=' + showid;
  18. x.get(url, function(s, x) {
  19. var evaled = false;
  20. if(s.indexOf('ajaxerror') != -1) {
  21. evalscript(s);
  22. evaled = true;
  23. }
  24. if(!evaled && (typeof ajaxerror == 'undefined' || !ajaxerror)) {
  25. if(x.showId) {
  26. x.showId.style.display = x.display;
  27. ajaxinnerhtml(x.showId, s);
  28. ajaxupdateevents(x.showId);
  29. if(x.autogoto) scroll(0, x.showId.offsetTop);
  30. }
  31. }
  32. ajaxerror = null;
  33. if(recall && typeof recall == 'function') {
  34. recall();
  35. } else if(recall) {
  36. eval(recall);
  37. }
  38. if(!evaled) evalscript(s);
  39. });
  40. }
  41. function _ajaxpost(formid, showid, waitid, showidclass, submitbtn, recall) {
  42. var waitid = typeof waitid == 'undefined' || waitid === null ? showid : (waitid !== '' ? waitid : '');
  43. var showidclass = !showidclass ? '' : showidclass;
  44. var ajaxframeid = 'ajaxframe';
  45. var ajaxframe = $(ajaxframeid);
  46. var curform = $(formid);
  47. var formtarget = curform.target;
  48. var handleResult = function() {
  49. var s = '';
  50. var evaled = false;
  51. showloading('none');
  52. try {
  53. s = $(ajaxframeid).contentWindow.document.XMLDocument.text;
  54. } catch(e) {
  55. try {
  56. s = $(ajaxframeid).contentWindow.document.documentElement.firstChild.wholeText;
  57. } catch(e) {
  58. try {
  59. s = $(ajaxframeid).contentWindow.document.documentElement.firstChild.nodeValue;
  60. } catch(e) {
  61. s = '内部错误,无法显示此内容';
  62. }
  63. }
  64. }
  65. if(s != '' && s.indexOf('ajaxerror') != -1) {
  66. evalscript(s);
  67. evaled = true;
  68. }
  69. if(showidclass) {
  70. if(showidclass != 'onerror') {
  71. $(showid).className = showidclass;
  72. } else {
  73. showError(s);
  74. ajaxerror = true;
  75. }
  76. }
  77. if(submitbtn) {
  78. submitbtn.disabled = false;
  79. }
  80. if(!evaled && (typeof ajaxerror == 'undefined' || !ajaxerror)) {
  81. ajaxinnerhtml($(showid), s);
  82. }
  83. ajaxerror = null;
  84. if(curform) curform.target = formtarget;
  85. if(typeof recall == 'function') {
  86. recall();
  87. } else {
  88. eval(recall);
  89. }
  90. if(!evaled) evalscript(s);
  91. ajaxframe.loading = 0;
  92. if(!BROWSER.firefox || BROWSER.safari) {
  93. $('append_parent').removeChild(ajaxframe.parentNode);
  94. } else {
  95. setTimeout(
  96. function(){
  97. $('append_parent').removeChild(ajaxframe.parentNode);
  98. },
  99. 100
  100. );
  101. }
  102. };
  103. if(!ajaxframe) {
  104. var div = document.createElement('div');
  105. div.style.display = 'none';
  106. div.innerHTML = '<iframe name="' + ajaxframeid + '" id="' + ajaxframeid + '" loading="1"></iframe>';
  107. $('append_parent').appendChild(div);
  108. ajaxframe = $(ajaxframeid);
  109. } else if(ajaxframe.loading) {
  110. return false;
  111. }
  112. _attachEvent(ajaxframe, 'load', handleResult);
  113. showloading();
  114. curform.target = ajaxframeid;
  115. var action = curform.getAttribute('action');
  116. action = hostconvert(action);
  117. curform.action = action.replace(/\&inajax\=1/g, '')+'&inajax=1';
  118. curform.submit();
  119. if(submitbtn) {
  120. submitbtn.disabled = true;
  121. }
  122. doane();
  123. return false;
  124. }
  125. function _ajaxmenu(ctrlObj, timeout, cache, duration, pos, recall, idclass, contentclass) {
  126. if(!ctrlObj.getAttribute('mid')) {
  127. var ctrlid = ctrlObj.id;
  128. if(!ctrlid) {
  129. ctrlObj.id = 'ajaxid_' + Math.random();
  130. }
  131. } else {
  132. var ctrlid = ctrlObj.getAttribute('mid');
  133. if(!ctrlObj.id) {
  134. ctrlObj.id = 'ajaxid_' + Math.random();
  135. }
  136. }
  137. var menuid = ctrlid + '_menu';
  138. var menu = $(menuid);
  139. if(isUndefined(timeout)) timeout = 3000;
  140. if(isUndefined(cache)) cache = 1;
  141. if(isUndefined(pos)) pos = '43';
  142. if(isUndefined(duration)) duration = timeout > 0 ? 0 : 3;
  143. if(isUndefined(idclass)) idclass = 'p_pop';
  144. if(isUndefined(contentclass)) contentclass = 'p_opt';
  145. var func = function() {
  146. showMenu({'ctrlid':ctrlObj.id,'menuid':menuid,'duration':duration,'timeout':timeout,'pos':pos,'cache':cache,'layer':2});
  147. if(typeof recall == 'function') {
  148. recall();
  149. } else {
  150. eval(recall);
  151. }
  152. };
  153. if(menu) {
  154. if(menu.style.display == '') {
  155. hideMenu(menuid);
  156. } else {
  157. func();
  158. }
  159. } else {
  160. menu = document.createElement('div');
  161. menu.id = menuid;
  162. menu.style.display = 'none';
  163. menu.className = idclass;
  164. menu.innerHTML = '<div class="' + contentclass + '" id="' + menuid + '_content"></div>';
  165. $('append_parent').appendChild(menu);
  166. var url = (!isUndefined(ctrlObj.attributes['shref']) ? ctrlObj.attributes['shref'].value : (!isUndefined(ctrlObj.href) ? ctrlObj.href : ctrlObj.attributes['href'].value));
  167. url += (url.indexOf('?') != -1 ? '&' :'?') + 'ajaxmenu=1';
  168. ajaxget(url, menuid + '_content', 'ajaxwaitid', '', '', func);
  169. }
  170. doane();
  171. }
  172. function _appendscript(src, text, reload, charset) {
  173. var id = hash(src + text);
  174. if(!reload && in_array(id, evalscripts)) return;
  175. if(reload && $(id)) {
  176. $(id).parentNode.removeChild($(id));
  177. }
  178. evalscripts.push(id);
  179. var scriptNode = document.createElement("script");
  180. scriptNode.type = "text/javascript";
  181. scriptNode.id = id;
  182. scriptNode.charset = charset ? charset : (BROWSER.firefox ? document.characterSet : document.charset);
  183. try {
  184. if(src) {
  185. scriptNode.src = src;
  186. scriptNode.onloadDone = false;
  187. scriptNode.onload = function () {
  188. scriptNode.onloadDone = true;
  189. JSLOADED[src] = 1;
  190. };
  191. scriptNode.onreadystatechange = function () {
  192. if((scriptNode.readyState == 'loaded' || scriptNode.readyState == 'complete') && !scriptNode.onloadDone) {
  193. scriptNode.onloadDone = true;
  194. JSLOADED[src] = 1;
  195. }
  196. };
  197. } else if(text){
  198. scriptNode.text = text;
  199. }
  200. document.getElementsByTagName('head')[0].appendChild(scriptNode);
  201. } catch(e) {}
  202. }
  203. function _ajaxupdateevents(obj, tagName) {
  204. tagName = tagName ? tagName : 'A';
  205. var objs = obj.getElementsByTagName(tagName);
  206. for(k in objs) {
  207. var o = objs[k];
  208. ajaxupdateevent(o);
  209. }
  210. }
  211. function _ajaxupdateevent(o) {
  212. if(typeof o == 'object' && o.getAttribute) {
  213. if(o.getAttribute('ajaxtarget')) {
  214. if(!o.id) o.id = Math.random();
  215. var ajaxevent = o.getAttribute('ajaxevent') ? o.getAttribute('ajaxevent') : 'click';
  216. var ajaxurl = o.getAttribute('ajaxurl') ? o.getAttribute('ajaxurl') : o.href;
  217. _attachEvent(o, ajaxevent, newfunction('ajaxget', ajaxurl, o.getAttribute('ajaxtarget'), o.getAttribute('ajaxwaitid'), o.getAttribute('ajaxloading'), o.getAttribute('ajaxdisplay')));
  218. if(o.getAttribute('ajaxfunc')) {
  219. o.getAttribute('ajaxfunc').match(/(\w+)\((.+?)\)/);
  220. _attachEvent(o, ajaxevent, newfunction(RegExp.$1, RegExp.$2));
  221. }
  222. }
  223. }
  224. }
  225. function _ajaxinnerhtml(showid, s) {
  226. if(showid.tagName != 'TBODY') {
  227. showid.innerHTML = s;
  228. } else {
  229. while(showid.firstChild) {
  230. showid.firstChild.parentNode.removeChild(showid.firstChild);
  231. }
  232. var div1 = document.createElement('DIV');
  233. div1.id = showid.id+'_div';
  234. div1.innerHTML = '<table><tbody id="'+showid.id+'_tbody">'+s+'</tbody></table>';
  235. $('append_parent').appendChild(div1);
  236. var trs = div1.getElementsByTagName('TR');
  237. var l = trs.length;
  238. for(var i=0; i<l; i++) {
  239. showid.appendChild(trs[0]);
  240. }
  241. var inputs = div1.getElementsByTagName('INPUT');
  242. var l = inputs.length;
  243. for(var i=0; i<l; i++) {
  244. showid.appendChild(inputs[0]);
  245. }
  246. div1.parentNode.removeChild(div1);
  247. }
  248. }