Contents ======== . JavaScript . AJAX . DHTML . CSS . XML . PHP . HTML JavaScript ========== * All functions in JavaScript are also objects.
- A block element. Starts a new line. - Embedded in the same line. document.getElementById('targetId').innerHTML = "some text"; Note: The element id can be send to a callback function: function displayText(id) { document.getElementById(id).innerHTML = "some text"; } // The JavaScript escape function will encode data for appending to the end of the URL var urlReadyText = escape(textField.value); // The callback will be called after 1/2sec setTimeout(callback, 500); AJAX (Asynchronous Java Script And XML) ==== // A script global object. One per request. var XMLHttpRequestObject = false; if (window.XMLHttpRequest) { // For Firefox XMLHttpRequestObject = new XMLHttpRequest(); } else if (window.ActiveXObject) { // For IE XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHttp"); } Request ------- var dataSource = 'whatever you send to the server'; // GET request (when getting data from the server) XMLHttpRequestObject.open("GET", dataSource); XMLHttpRequestObject.send(null); // POST request (when sending data to the server - more secure than GET) XMLHttpRequestObject.open("POST", url); // as text XMLHttpRequestObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); // as XML XMLHttpRequestObject.setRequestHeader('Content-Type', 'text/xml'); XMLHttpRequestObject.send(dataSource); // HEAD request (headers) XMLHttpRequestObject.open("HEAD", dataSource); XMLHttpRequestObject.send(null); Response -------- // For text content var textDocument = XMLHttpRequestObject.responseText; // For XML content var xmlDocument = XMLHttpRequestObject.responseXML; var options = xmlDocument.getElementsByTagName('option'); var selectControl = document.getElementById('optionList'); for (var i = 0; i < options.length; i++ ) { selectControl.options[i] = new Option(options[i].firstChild.data); } // HEAD response // All headers var headers = XMLHttpRequestObject.getAllResponseHeaders(); // Last modified data (etc) XMLHttpRequestObject.getResponseHeader("Last-Modified"); Does a URL Exist ---------------- * Could be used to check whether the server is down XMLHttpRequestObject.onreadystatechange = function() { if (XMLHttpRequestObject.readyState == 4) { if (XMLHttpRequestObject.status == 200) { // Exists } else if (XMLHttpRequestObject.status == 404) { // URL does not exist } } } Returning JavaScript From Server -------------------------------- eval(XMLHttpRequestObject.responseText); Returning JavaScript Object --------------------------- // var text = XMLHttpRequestObject.responseText; var text = "{method: 'adder', operand1: 2, operand2: 3};"; var jSObject; eval('jSObject = ' + text); eval(jSObject.method + '(' + jSObject.operand1 + ',' + jSObject.operand2 + ');'); Calling Different Domain ------------------------ Q: When called by the client browser, causes a warning message to pop-up. A: Call it from your server, and return the results to the client. Debugging --------- Install GreaseMonkey and XMLHttpRequest debugger script. Turning Off Caching ------------------- In the HTML header: header("Cache-Control", "no-cache"); header("Pragma", "no-cache"); header("Expires", "-1"); Alter the URL the application is requesting from the server: var myUrl = "data.php?name=steve" + "&t=" + new Date().getTime(); DHTML ===== // Makes the event browser independant (Firefox/IE) // Call it at the beginning of every function that handles mouse events function MouseEvent(e) { if(e) { this.e = e; } else { this.e = window.event; } if(e.pageX) { this.x = e.pageX; } else { this.x = e.clientX; } if(e.pageY) { this.y = e.pageY; } else { this.y = e.clientY; } if(e.target) { this.target = e.target; } else { this.target = e.srcElement; } } // Adds a listener. Browser independant (Firefox/IE) function addListener(type, callback) { if (document.addEventListener) { document.addEventListener(type, callback, false); } else if (document.attachEvent) { document.attachEvent("on" + type, callback, false); } } // Removes a listener. Browser independant (Firefox/IE) function removeListener (type, callback) { if (document.removeEventListener) { document.removeEventListener(type, callback, false); } else if (document.detachEvent) { document.detachEvent("on" + type, callback, false); } } CSS (Cascading Style Sheets) ===