Request-Function for Widgets: Syntax: ajax(file, [func_target], [use_req_num]) - file: server address (for example: "http://example.com/test.php") - func_target: optional, if func_target is set, the response of the request will be send to the function as argument - use_req_num: optional, if use_req_num is set, the function will check whether a response was sent Examples: 1. Example: ajax('http://example.com/'); // a request will be send to http://example.com 2. Example: function get_resp(resp) { alert(resp); } ajax('http://example.com/',get_resp); // The response will be given to function get_resp. // So the content of the website will be displayed in the alert box. 3. Example: function get_resp(resp) { alert(resp); } ajax('http://example.com/',get_resp,true); // Same like before but if a connection error occurs or the website // returns an empty response the error function is called after 10 seconds 4. Example: function get_resp(argument1, resp) { // Currying adds the response as last argument alert(argument1 + resp); } ajax('http://example.com/',get_resp.curry('another argument for the function'),true); // Same like before but another Argument is passed to the target function /******************************************************************************* ************************************* CODE ************************************* *******************************************************************************/ var req_num = 0; // req_num checks whether the connection-ID is still valid function checkAjaxError(tmp_req_num) { // the function which will be called after 10 sec and checks if a response was sent if (req_num == tmp_req_num) { req_num++; alert('Error! Missing or slow Internet connection.'); /* Here you can insert the code, which will be run when an error occurs */ } } function ajax(file, func_target, use_req_num) { // use_req_num if switchLoading is used var tmp_ajax_obj = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'); // runs in IE too ;) tmp_ajax_obj.open('post', file, true); // post => no browser-cache req_num++; var tmp_req_num = req_num; if (func_target) { if (use_req_num) setTimeout('checkAjaxError('+tmp_req_num+');',10000); // after 10 secs => connection error, change the time to get the error earlier tmp_ajax_obj.onreadystatechange = function(){ if(tmp_ajax_obj.readyState == 4 && (!use_req_num || tmp_req_num == req_num) && tmp_ajax_obj.responseText != '') { req_num++; func_target(tmp_ajax_obj.responseText); // target function is called } } } tmp_ajax_obj.send(null); } /* Thanks to molily: ** http://forum.de.selfhtml.org/archiv/2009/7/t188707/#m1256395 */ Function.prototype.curry = (function (slice) { // Currying for JavaScript return function () { var func = this, args = slice.apply(arguments); return function () { return func.apply(this, args.concat(slice.apply(arguments))); }; }; })(Array.prototype.slice);