*/ /** * TYPO3 XMLHTTP class (new in TYPO3 4.0.0) * This class contains two main parts: * (1) generation of JavaScript code which creates an XMLHTTP object in a cross-browser manner * (2) generation of XML data as a reply * * @author Sebastian Kurfuerst * @package TYPO3 * @subpackage t3lib */ class t3lib_ajax { /** * Gets the JavaScript code needed to handle an XMLHTTP request in the frontend. * All JS functions have to call ajax_doRequest(url) to make a request to the server. * USE: * See examples of using this function in template.php -> getContextMenuCode and alt_clickmenu.php -> printContent * * @param string JS function handling the XML data from the server. That function gets the returned XML data as parameter. * @param string JS fallback function which is called with the URL of the request in case ajax is not available. * @param boolean If set to 1, the returned XML data is outputted as text in an alert window - useful for debugging, PHP errors are shown there, ... * @return string JavaScript code needed to make and handle an XMLHTTP request */ function getJScode($handlerFunction, $fallback='', $debug=0) { // Init the XMLHTTP request object $code = ' function ajax_initObject() { var A; try { A=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { A=new ActiveXObject("Microsoft.XMLHTTP"); } catch (oc) { A=null; } } if(!A && typeof XMLHttpRequest != "undefined") { A = new XMLHttpRequest(); } return A; }'; // in case AJAX is not available, fallback function if($fallback) { $fallback .= '(url)'; } else { $fallback = 'return'; } $code .= ' function ajax_doRequest(url) { var x; x = ajax_initObject(); if(!x) { '.$fallback.'; } x.open("GET", url, true); x.onreadystatechange = function() { if (x.readyState != 4) { return; } '.($debug?'alert(x.responseText)':'').' var xmldoc = x.responseXML; var t3ajax = xmldoc.getElementsByTagName("t3ajax")[0]; '.$handlerFunction.'(t3ajax); } x.send(""); delete x; }'; return $code; } /** * Function outputting XML data for TYPO3 ajax. The function directly outputs headers and content to the browser. * * @param string $innerXML XML data which will be sent to the browser * @return void */ function outputXMLreply($innerXML) { // AJAX needs some XML data header('Content-Type: text/xml'); $xml = ' '.$innerXML.''; echo $xml; } } if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_ajax.php']) { include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_ajax.php']); } ?>