2 /***************************************************************
5 * (c) 2004-2005 Kasper Skaarhoj (kasperYYYY@typo3.com)
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
28 * Contains the class "t3lib_ajax" containing functions for doing XMLHTTP requests to the TYPO3 backend and as well for generating replys. This technology is also known as ajax.
29 * Call ALL methods without making an object!
31 * IMPORTANT NOTICE: The API the class provides is still NOT STABLE and SUBJECT TO CHANGE!
32 * It is planned to integrate an external AJAX library, so the API will most likely change again.
34 * @author Sebastian Kurfuerst <sebastian@garbage-group.de>
38 * TYPO3 XMLHTTP class (new in TYPO3 4.0.0)
39 * This class contains two main parts:
40 * (1) generation of JavaScript code which creates an XMLHTTP object in a cross-browser manner
41 * (2) generation of XML data as a reply
43 * @author Sebastian Kurfuerst <sebastian@garbage-group.de>
49 * Gets the JavaScript code needed to handle an XMLHTTP request in the frontend.
50 * All JS functions have to call ajax_doRequest(url) to make a request to the server.
52 * See examples of using this function in template.php -> getContextMenuCode and alt_clickmenu.php -> printContent
54 * @param string JS function handling the XML data from the server. That function gets the returned XML data as parameter.
55 * @param string JS fallback function which is called with the URL of the request in case ajax is not available.
56 * @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, ...
57 * @return string JavaScript code needed to make and handle an XMLHTTP request
59 function getJScode($handlerFunction, $fallback='', $debug=0) {
60 // Init the XMLHTTP request object
62 function ajax_initObject() {
65 A=new ActiveXObject("Msxml2.XMLHTTP");
68 A=new ActiveXObject("Microsoft.XMLHTTP");
73 if(!A && typeof XMLHttpRequest != "undefined") {
74 A = new XMLHttpRequest();
78 // in case AJAX is not available, fallback function
85 function ajax_doRequest(url) {
88 x = ajax_initObject();
92 x.open("GET", url, true);
94 x.onreadystatechange = function() {
95 if (x.readyState != 4) {
98 '.($debug?
'alert(x.responseText)':'').'
99 var xmldoc = x.responseXML;
100 var t3ajax = xmldoc.getElementsByTagName("t3ajax")[0];
101 '.$handlerFunction.'(t3ajax);
112 * Function outputting XML data for TYPO3 ajax. The function directly outputs headers and content to the browser.
114 * @param string $innerXML XML data which will be sent to the browser
117 function outputXMLreply($innerXML) {
118 // AJAX needs some XML data
119 header('Content-Type: text/xml');
120 $xml = '<?xml version="1.0"?>
121 <t3ajax>'.$innerXML.'</t3ajax>';
128 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE
]['XCLASS']['t3lib/class.t3lib_ajax.php']) {
129 include_once($TYPO3_CONF_VARS[TYPO3_MODE
]['XCLASS']['t3lib/class.t3lib_ajax.php']);