11.07.2015 Views

AJAX and PHP

AJAX and PHP

AJAX and PHP

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 8On the client, this data will be parsed <strong>and</strong> transformed to the HTML grid using an XSLtransformation. This code was tested with Mozilla <strong>and</strong> Internet Explorer, which at the time ofwriting supported the required functionality. Opera is expected to support XSL Transformationsstarting with version 9.The XSL transformation code is defined in grid.xsl. Please see Appendix C athttp://ajaxphp.packtpub.comfor a primer into the world of XSL, <strong>and</strong> refer one of the manyavailable books <strong>and</strong> online resources for digging into the details. XSL is a really big subject, so beprepared for a lot of learning if you intend to master it.The first function in the client script, grid.js, is init(). This function checks if the user'sbrowser has the necessary features to perform the XSL transformation:// eveything starts herefunction init(){// test if user has browser that supports native XSLT functionalityif(window.XMLHttpRequest && window.XSLTProcessor && window.DOMParser){// load the gridloadStylesheet();loadGridPage(1);return;}// test if user has Internet Explorer with proper XSLT supportif (window.ActiveXObject && createMsxml2DOMDocumentObject()){// load the gridloadStylesheet();loadGridPage(1);// exit the functionreturn;}// if browser functionality testing failed, alert the useralert("Your browser doesn't support the necessary functionality.");}This function allows continuing if the browser is either Internet Explorer (in which case the useralso needs a recent MSXML version), or a browser that natively supports the XMLHttpRequest,XSLTProcessor, <strong>and</strong> DOMParser classes.The second function that is important to underst<strong>and</strong> is loadStylesheet(). This function is calledonce when the page loads, to request the grid.xsl file from the server, which is loaded locally.The grid.xls file is loaded using a synchronous call, <strong>and</strong> then is stored using techniques specificto the user's browser, depending on whether the browser has native functionality, or it is InternetExplorer, in which case an ActiveXObject is used:// loads the stylesheet from the server using a synchronous requestfunction loadStylesheet(){// load the file from the serverxmlHttp.open("GET", xsltFileUrl, false);xmlHttp.send(null);// try to load the XSLT documentif (this.DOMParser) // browsers with native functionality{var dp = new DOMParser();stylesheetDoc = dp.parseFromString(xmlHttp.responseText, "text/xml");}else if (window.ActiveXObject) // Internet Explorer?219

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!